bullet and bomb collisions

BlitzPlus Forums/BlitzPlus Beginners Area/bullet and bomb collisions

iaqsuk(Posted 2012) [#1]
I have a space invaders like 2d game. Here's my code for collisions from alien bomb to player ship, and it works:
;check each bullet for contact with alien then creates explosion picture too.
;update And draw alien bombs
For bombs.bomb = Each bomb
;sets alien bombs to move from alien ship towards player ship
bombs\y = bombs\y + 4
;draws bomb
DrawImage img_bomb,bombs\x,bombs\y
;checks for collision with alien bombs and players ship
If ImagesCollide(img_bomb,bombs\x,bombs\y,0,img_ship,x,y,0) Then
;plays player explosion sound once alien bombs hits player ship
PlaySound(snd_shithit)
;sets lives minus by 1 if alien bomb hits player ship and updated in lives screen
lives = lives - 1
;delets bombs once hits player ship
Delete bombs
;generates player ship explosion images once alien bombs hit player
ship
DrawImage img_shipd,x,y
;checks to see if alien bombs passes 600 y coordinate
ElseIf bombs\y > 600 Then
;deletes bombs if alien bombs is greater then 600 y coordinate
Delete bombs
EndIf
Next
I am trying to create collisions for player bullets and alien bombs. I was told this is possible, just need to create another cycle to update bullets and alien bomb?

Here's my attempt on bullets and bombs code:
;update And draw alien bombs
For bombs.bomb = Each bomb
bombs\y = bombs\y + 4
;draws bomb
DrawImage img_bomb,bombs\x,bombs\y
next
;checks for collision with alien bombs and players ship
for b.bullet = each bullet
b\x=x
b\y=b\y -5
drawimage img_bullet,b\x,b\y next
If ImagesCollide(img_bomb,bombs\x,bombs\y,0,img_bullet,b\x,b\y,0) Then
;plays player explosion sound once alien bombs hits player ship
PlaySound(snd_shithit)
;deletes bombs and bullets once hits each other
Delete bombs
delete b
;generates player ship explosion images once alien bombs hit player ship
DrawImage img_bulletbombdeath,x,y
;checks to see if alien bombs passes 600 y coordinate
ElseIf bombs\y > 600 Then
;deletes bombs if alien bombs is greater then 600 y coordinate
Delete bombs
ElseIf bullet\y < 0 Then
;deletes bullets if bullets is less then 0 y coordinate
delete b
EndIf
Next
I get a object does not exist even it is? Help, Thanks


Matty(Posted 2012) [#2]
Very hard to read that code without proper formatting and in a codebox but it looks like you are still referring to the "bombs" instance after you delete it....the type instance will be null after you delete it - and it won't exist anymore so checking its "y" value after deleting it is the wrong way to go about it.


iaqsuk(Posted 2012) [#3]
Hi Matty, I'll relook at the code again to try and figure it out. sorry about the layout of the code. I tried to fix it but the textarea was acting funny when I was posting it at the time. will let you know if I figure it out. I think I have a idea why its not working. But started on anothe project. The Space Invaders like game, the above code was my first game I created and I get everything working except I wanted to add the bullet to bombs imagescollide. Thanks for the reply. iaqsuk


iaqsuk(Posted 2012) [#4]
Hi Matty, and everyone else. I finally figured out the bullets to alien bombs collision: here's the code:
;update bullets and draw them, also deletes bullets with alien bullets if collides
For b.bullet = Each bullet
b\y = b\y - 5
DrawImage img_bullet,b\x,b\y
For ab.abullet = Each abullet
ab\y = ab\y + 4
DrawImage img_abullet,ab\x,ab\y
If ImagesCollide(img_bullet,b\x,b\y,0,img_abullet,ab\x,ab\y,0) Then
DrawImage img_bulletabulletd,x,y
PlaySound(snd_bulletabullet)
Delete ab
Delete b
EndIf
Next
Next
did the trick. just added the imagescollide from the original update b.bullet = each bullets. Very glad it worked. done with this game, now gonna work on another game. Good Luck all. iaqsuk