imagecollide / type deletion

Blitz3D Forums/Blitz3D Beginners Area/imagecollide / type deletion

I have some problems with my code for a new game I'm making. Its the first time that I'm using types and the following code produces an error message:

[code]
for e.enemy = each enemy
for b.bullet = each bullet
if imagerectcollide(enemyImg,e\x,e\y,0,b\x,b\y,8,1) then
delete b
delete e
end if
next
next
[\code]

It gives me this error: Object does not exist!

What am I doing wrong?

You are deleting e before you are finished with it. Set a flag and delete e in the outer loop.

its because the loops are trying to perform actions on objects which have been deleted (the enemy object).

quickest solution:

add a field to the enemy type like
 field dead 
and flag it 'true' if imagescollide is satisfied. like this:

[code]
if imagerectcollide(enemyImg,e\x,e\y,0,b\x,b\y,8,1) then
e\dead=true
delete b
[code]

next, check whether e\dead=true outside of this loop and, if so delete it.

i'm rambling now, so i'll just show you :)

[code]
for e.enemy = each enemy
for b.bullet = each bullet
if imagerectcollide(enemyImg,e\x,e\y,0,b\x,b\y,8,1) then
e\dead=true
delete b
end if
next
if e\dead then delete e
next
[\code]

this should work.

have fun.

Thanks guys, I didn't think about that, well that just goes to tell that after hours of programming you tend to overlook the most obvious. Maybe now I can get on with my game.

or you could just exit the bullet loop like:
delete e
exit

well, yes I guess I could, but what if more than one bullet hits the enemy. I would like it to remove any bullets hitting. I changed my code as per DarkNature and DJWoodgate and it worked out just fine. Thanks again guys.