Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / Getting sprite numbers

Author
Message
Code Stealer
20
Years of Service
User Offline
Joined: 12th Feb 2004
Location:
Posted: 28th Feb 2004 15:07
If I want to test if my player sprite has hit another sprite, I use 0 as the second number in the command, like this

if sprite collision(playersprite,0)<0 then hit=1



My question is, how do I get the sprite number that Ive collided with without having to run a collision test for every sprite?

I tried using a variable instead of 0, but it just retains its value of 0.


GIve me more power!
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 28th Feb 2004 16:45
You could use the distance formula to find out which is the nearest sprite. Or just to check for collisions between sprites that are near to you.

Pseudo Code.........

Your sprite x/y = x1,y1
Enemy x/y = x2,y2

if distance(x1,x2,y1,y2) < 32 or whatever

Endif


function distance(x1,x2,y1,y2)
endfunction sqrt(((x2-x1)^2)+((y2-y1)^2))

Code Stealer
20
Years of Service
User Offline
Joined: 12th Feb 2004
Location:
Posted: 28th Feb 2004 17:05
Sorry, can you explain that a bit more? I like the idea, but surely you have to check all the sprites, to find out which is the nearest?
I mean, how do I get a return number for the actual sprite Ive collided with?

If I have,m say, 200 sprites running, and sprite1 has hit one of them, but I want to know which number it is, without going"check sprite2, is ther ea collision, check sprite 3, is there a collision etc all the way to 200. It seems the example above assumes I know the number of the second sprite, or at least its then just gonna be that on, whereas what if I want to do some other suff and its linked to my sprite number? SOrry to be a bore....

GIve me more power!
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 28th Feb 2004 17:18
You are right, it's not much better the way that I have posted it. You have to do a lot of checks. Here's a better idea...

If you are still using a map, you could just store all of your sprites as a number in a similar map. That would be 2 maps, but the second map just storing the sprite numbers. This would work so long as 2 sprites can't share the same tile. So wherever your player is standing, he is surrounded by 8 tiles. Just check those 8 tiles for a sprite number. Then check for collision with those stored numbers.

CloseToPerfect
21
Years of Service
User Offline
Joined: 20th Dec 2002
Location: United States
Posted: 28th Feb 2004 18:45 Edited at: 28th Feb 2004 18:46
all you neeed to do C.S. is pass the function into a variable and the variable well store what sprite has collided with your player sprite.

HitBySpriteNo = sprite collision(playersprite,0)

if you need to check collision first try something like this

if sprite collision(playersprite,0)<0
hit = 1
HitBySpriteNo = sprite collision(playersprite,0)
endif

it now passes out 2 aruguments (hit and HitBySpriteNo)

CTP

RGT may be gone but the best DBP forum is still alive and kicking, check it out.
http://www.dannywartnaby.co.uk/rgt/
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 28th Feb 2004 19:02
Yes, I was being dumb. I forgot about the sprite 0 collision check, which returns any sprite number that is overlapping your sprite. It's just 1 check.

Sorry about that, I rarely use sprited in groups. The most I have ever used is 4. I tend to use images mostly.

Code Stealer
20
Years of Service
User Offline
Joined: 12th Feb 2004
Location:
Posted: 28th Feb 2004 22:46
Hey thanks, that worked out fine. Its wierd this programming lark, but I like it. Solvimg problems like that gives you a good sence of achievement. Again thanks for the help. Im still not sure if I fully understand the logic of it though. How does it extract the correct number when youve put in the extra command line? Oh, well, I think I get it. I managed to get it working, so I guess the theory will follow tha practice!

GIve me more power!
Cros Jovil
20
Years of Service
User Offline
Joined: 27th Jan 2004
Location:
Posted: 29th Feb 2004 17:51
code steaker you could preform a loop by recording the number of sprites you have

for a=1 to numofsprites
if sprite hit (plyrsprt,a)>0 then hit=1
next a
Cros Jovil
20
Years of Service
User Offline
Joined: 27th Jan 2004
Location:
Posted: 29th Feb 2004 17:54
better yet do

a=1
repeat
if sprite hit(plyrsprt,a) then hit=1
a=a+1
until hit=1 or a=maxsprt

but then again I'm just a noob
Cros Jovil
20
Years of Service
User Offline
Joined: 27th Jan 2004
Location:
Posted: 29th Feb 2004 17:55
then just record a
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 29th Feb 2004 20:11
You should read the other posts.....It can be done in a single check. I made the same mistake earlier.

Login to post a reply

Server time is: 2024-05-17 06:58:22
Your offset time is: 2024-05-17 06:58:22