blitzbasic

This topic is 5172 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

hi guys, im new here, and new to game programming too. Anyway, my code for a 2d shooter game Im making is having problems with the collision. Here is the function im having problems with:
;moves the bullets and detects collision
Function UpdateBullets()

;check every bullet
For bullet.bullet = Each bullet

;check if the bullet is from the player
If bullet\from = 1

;if player is facing right
If player\direction = MRIGHT
;move the bullet right
bullet\x = bullet\x + 20
EndIf

;if player is facing left
If player\direction = MLEFT
;move the bullet left
bullet\x = bullet\x - 20
EndIf

;End "if bullet\from = 1"
EndIf

;check if the bullet is from the enemy
If bullet\from = 2
;move the bullet left
bullet\x = bullet\x - 20
EndIf

;If bullet is from the player then draw the correct image
If bullet\from = 1
DrawImage (bulletimage, bullet\x, bullet\y)
Else
DrawImage (bulletsimage,bullet\x, bullet\y)
EndIf

;if the bullet is from the player
If bullet\from = 1
;CHECK EVERY ENEMY!
For tank.tank = Each tank

;check to see that the player's bullet and the enemy collided
If ImagesCollide(bulletimage, bullet\x, bullet\y, 0, tankimage,tank\x,tank\y,0)

;subtract a hitpoint
tank\hits = tank\hits - 1

;if there are no hitpoints left delete enemy
If tank\hits <= 0
Delete tank
EndIf
;and the bullet
Delete bullet

EndIf
Next
EndIf

;if the bullet is from an enemy
If bullet\from = 2

;and the player and enemy's bullet collide
If Imagescollide(bimage,bullet\x,bullet\y,0,playerimage,player\x,player\y,0)

;subtract a hitpoint
player\hits = player\hits - 1

;if the player's hits = 0 delete the player
If player\hits <= 0
Delete player

;end the program
End
EndIf

EndIf

;delete the bullet if it collided
Delete bullet

EndIf
Next
End Function
When I run the program, it highlights this: If Imagescollide(bimage,bullet\x,bullet\y,0,playerimage,player\x,player\y,0) and says "object does not exist". Keep in mind this is not the entire game. it is just 1 function. Thanks in advance. [Edited by - Fruny on August 25, 2005 10:45:43 AM]

Share on other sites

Try printing out the values in bimage and playerimage. If they are 0 then (I believe) the image hasn't been loaded properly.

Share on other sites
ok, I made sure That those images exist.And they do. I really don't know what the problem is. . . . .

Share on other sites
You have "bimage" in the line with the error, but elsewhere you have "bulletimage". Is that perhaps the problem?

edit - I also noticed you have "bulletsimage" in another place. That might be a problem waiting to happen, as well.

[Edited by - Dave Hunt on August 25, 2005 9:20:16 AM]

Share on other sites
well, actually, i just made it bimage so it would fit on 1 line in this site. And im pretty sure there is something wrong with the player. . . .

In my program,the player's bullets are bulletimage. The enemy's bullets are bulletsimage. Im sorry, it is kinda confusing. so replace the top "bimage" with "bulletimage" and the bottom one with "bulletsimage".

Share on other sites
Ok. Understood.

In your outer loop, if the player is killed, you delete the player object, but the loop continues. So, on the next iteration, you would be referencing the deleted object.

Also, in your inner your loop, if the bullet was from the player and hits an enemy, you delete the bullet. Then you continue checking that bullet against the next enemy. When the inner loop is finished, you then check that (potentially) deleted bullet to see if it was from the player.

From what I can see, you have a number of places where you might be referencing deleted objects.

Without seeing the rest of the code, I can't say what else might be wrong with your objects.

As an aside, when posting code, try surrounding the code with "[ source ]" and "[ /source ]" (without the quotes and spaces). That will maintain your formatting and make it easier for us to read.

Share on other sites
yeah, sorry about that, im new here, so I didn't know how to do that. Thanks for your help, im gonna fool around with the code now and try and make it work.

Oh yeah, Here is all the code in case you need need it.

Graphics 800,600,0,2

;set the buffer, automidhandle and seeding
SetBuffer BackBuffer()
AutoMidHandle True
SeedRnd MilliSecs()

;directions the player is facing
Const MLEFT = 1
Const MRIGHT = 2
Const RKEY = 205
Const LKEY = 203
Const UKEY = 200
Const DKEY = 205
Const SPACE = 57

;types__________
Type player
Field x,y
Field frame
Field direction
Field hits
End Type

Type bullet
Field x,y
Field from
End Type

Type tank
Field x,y
Field hits
Field xv,yv
End Type
;\types_______________

;create the player
Global player.player = New player

;player's coordinates, frame, direction, and hits
player\x = 300
player\y = 300
player\frame = 0
player\direction = MLEFT
player\hits = 1

;create the enemy
Global tank.tank = New tank

;tank's coordinates, hits, and velocity
tank\x = 400
tank\y = 300
tank\hits = 15
tank\xv = Rand(-5,5)
tank\yv = Rand(-5,5)

;_________________________________________________________________________

;Mainloop
While Not KeyDown(1)

;clear the screen
Cls

;background
TileBlock Grassimage

;move player left
If KeyDown(LKEY)
player\x = player\x - 5
player\frame = (player\frame + 1 ) Mod (4) + (4 * (player\direction) -4)
player\direction = MLEFT
EndIf

;move player right
If KeyDown(RKEY)
player\x = player\x + 5
player\frame = (player\frame + 1 ) Mod (4) + (4 * (player\direction) -4)
player\direction = MRIGHT

EndIf

;jumping (not finished)
If KeyHit(UKEY)
player\y = player\y - 20
player\y = player\y + 20
EndIf

;shoot the bullet!
If KeyDown(SPACE)
Createbullet(player\x + 36, player\y + 22, 1)

EndIf

;call all the necessary functions
UpdateBullets()
drawtank()
Drawplayer()

AI()

;flip it and wait a fraction of a sec
Flip
Delay 50
Wend
;_________________________________________________________________________

;moves the bullets and detects collision
Function UpdateBullets()

;check every bullet
For bullet.bullet = Each bullet

;check if the bullet is from the player
If bullet\from = 1

;if player is facing right
If player\direction = MRIGHT
;move the bullet right
bullet\x = bullet\x + 20
EndIf

;if player is facing left
If player\direction = MLEFT
;move the bullet left
bullet\x = bullet\x - 20
EndIf

;End "if bullet\from = 1"
EndIf

;check if the bullet is from the enemy
If bullet\from = 2
;move the bullet left
bullet\x = bullet\x - 20
EndIf

;If bullet is from the player or enemy then draw the correct image

If bullet\from = 1
DrawImage (bulletimage, bullet\x, bullet\y)
Else

DrawImage (bulletsimage,bullet\x, bullet\y)
EndIf

;if the bullet is from the player
If bullet\from = 1
;CHECK EVERY ENEMY!
For tank.tank = Each tank

;check to see that the player's bullet and the enemy collided

If ImagesCollide(bulletimage, bullet\x, bullet\y, 0, tankimage, tank\x, tank\y,0)

;subtract a hitpoint
tank\hits = tank\hits - 1

;if there are no hitpoints left delete enemy
If tank\hits <= 0
Delete tank
EndIf
;and the bullet
Delete bullet

EndIf

Next
EndIf

;if the bullet is from an enemy
If bullet\from = 2

;and the player and enemy's bullet collide
If ImagesCollide(bulletsimage,bullet\x,bullet\y,0,playerimage,player\x,player\y,0)
;subtract a hitpoint
player\hits = player\hits - 1

;if the player's hits = 0 delete the player

If player\hits <= 0
Delete player

;end the program
End
EndIf

EndIf

;delete the bullet if it collided
Delete bullet

EndIf

Next
End Function

Function drawtank()
;this just creates the enemy and player

For tank.tank = Each tank

DrawImage tankimage, tank\x,tank\y

Locate 0,0
Print "Tank hits: " + tank\hits

Next

End Function

Function drawplayer()
For player.player = Each player

DrawImage playerimage, player\x, player\y, player\frame

Next

End Function

Function AI()

;the poor enemy AI
For enemy.tank = Each tank

If enemy\y = enemy\y
Createbullet(Enemy\x + 36, enemy\y + 22, 2)
EndIf

enemy\x = enemy\x + enemy\xv

If enemy\x >=790
enemy\xv = -enemy\xv
EndIf

If enemy\x <= 10
enemy\xv = -enemy\xv
EndIf

Next

End Function

;_________________________________________________________________________

;creates the bullet
Function Createbullet(x,y,from)

bullet.bullet = New bullet

bullet\x = x
bullet\y = y

bullet\from = from

End Function

[Edited by - Porsche911 on August 25, 2005 1:36:44 PM]

Share on other sites
UPDATE:

When I put a For. . . . Next loop before the

If ImagesCollide(bulletsimage,bullet\x,bullet\y,0,playerimage,player\x,player\y,0)

The program works, but If I try to move, It highlights either

Player\x = player\x - 5 (If I am trying to move left)

or

player\x = player\x + 5 (If I am trying to move right)

Also, the enemies bullets don't move

Share on other sites
The only other thing I see is in the section that handles bullets from the enemy. You have the "delete bullet" statement AFTER the EndIf for the collision test. So, you will basically delete every enemy bullet whether it hits the player or not.

You also still have the case where, after deleting a bullet that hits a tank, you then check to see if that bullet came from the player, which should cause an error.

I'm not sure why it would be stopping in your movement code, unless the player object was being deleted somewhere. I don't see anything that would cause that.

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11