help with imagecollision pls

Blitz3D Forums/Blitz3D Beginners Area/help with imagecollision pls

767pilot(Posted 2004) [#1]
Can anyone give me some pointers with the below game please

http://www.geocities.com/gjpollitt/game.zip

I want to test if the laser hits the bat, how can I do this properly? The code as it stands detects a hit even when the laser is on the other side of the screen !!

If it hits the bat, how do I delete the bat image?

How would I approach this code if I had 10 bats? Would I have to create a type array? How would I add/delete bats from this?

Thanks for any help I may receive :)


big10p(Posted 2004) [#2]
Put 'Global manfirerange' at the top of you code somewhere.


767pilot(Posted 2004) [#3]
Thanks, that works fine now

Any ideas how to delete the images?


semar(Posted 2004) [#4]
Any ideas how to delete the images?

Don't draw it on the screen.

You may need a flag that indicates if an image should be drawn or not:
if imagescollide(bat_img,bat_x,bat_y,laser_img,laser_x,laser_y) then
bat_alive = false
endif

if bat_alive then
drawimage bat, batx, baty
endif


If you have more than one bat, yes you may use an array or a type structure, so that you have a collection of fields for each bat.
type t_bat
field image
field x,y
field alive
end type
global bat.t_bat


So you can later write something like:
for bat.t_bat = each t_bat
if bat\alive then
drawimage bat\image,bat\x,bat\y
endif
next

Hope this helps.

Sergio.


767pilot(Posted 2004) [#5]
http://www.geocities.com/gjpollitt/game.zip

When looping through each of the bat types why can i not access bat id 1 and bat id 2? When debugging the first bat id is 2 but then for the second one returns 0. Why is this?

thanks


big10p(Posted 2004) [#6]
Because you have a typo in your code, probably from copying and pasting existing code. You have:
Global bat2.bat=New bat
bat2\frame=0
bat2\x=40
bat2\y=60
bat2\gfx=gfx_bat
bat2\lft =0
bat2\up =0
bat2\alive=1
bat1\id =2
bat1\xmax =40
bat1\ymax =220
bat1\xmin =40
bat1\ymin=60


when it should be:

Global bat2.bat=New bat
bat2\frame=0
bat2\x=40
bat2\y=60
bat2\gfx=gfx_bat
bat2\lft =0
bat2\up =0
bat2\alive=1
bat2\id =2
bat2\xmax =40
bat2\ymax =220
bat2\xmin =40
bat2\ymin=60


It's a common mistake. :)


767pilot(Posted 2004) [#7]
crikey, why didnt i spot that?

thanks guys


767pilot(Posted 2004) [#8]
www.geocities.com/gjpollitt/game.zip

Can anyone please tell me why when the player collides with the bat the bat disappears? That should only happen when the laser hits the bat. The images are loaded correctly as when in debug mode each image has its own unique id number.

Thanks


big10p(Posted 2004) [#9]
It's happening because you're telling it to. :)

In your check_collisions() function, you have this line:
	c= ImagesCollide (gfx_manfire,manfirerange,man\y+35,0,b\gfx,b\x,b\y,0)

which checks to see if the image of the man firing collides with a bat image. Note that the image of the man firing doesn't have to be currenly displayed! - the check will still be done.


767pilot(Posted 2004) [#10]
OK then, so how would I get around that?

I understood that I was checking for an image that collided with another image. If that image isnt loaded or visible why would it be checked? I dont understand that part

Thanks


big10p(Posted 2004) [#11]
The 2 images used with ImagesCollide() do have to be valid images (i.e. loaded or created in code) but don't have to actually be displayed on screen. It's more like saying "if I draw these 2 images at these positions, will they collide?".

This makes ImagesCollide() much more versatile. Imagine if you had a scrolling game and you wanted to check for collisions between sprites that are off screen!

To correct your code, I'd probably just set a flag when the player fires, and then only do the collision check between lazer and bat when that flag is set. Remember to reset the flag when the player stops firing. :)