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.

Newcomers AppGameKit Corner / Getting a list of all sprites that have collided with my player sprite

Author
Message
Scifictus
9
Years of Service
User Offline
Joined: 7th Feb 2015
Location:
Posted: 21st Feb 2015 14:25
Hi farmorelearnedthanI people!

I am writing some code which I suspect could be better. I am going through all my various sprites (coins, bonuses, barriers etc.) one by one using a for next loop and the array ID for those sprites, to determine if any of them have a collision with my player sprite. e.g.

for t = 1 to max_coins
if getspritecollision(player,coin[t])
do stuff..
next t.
[the repeat above code for every sprite I know could be on screen]

Is there a better way? For example, can I request a list of all sprite IDs that have collided with my player sprite (i.e. a function that would return a list/array of those sprite IDs that I could then step through). e.g.

array[] = getspritecollision(player)

I could then just interrogate the array for the sprite IDs that have collided...rather than looping through all known sprites as per my first example. Is ther a function/approach that would improve my current structure?
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Feb 2015 09:42
I tend to use Physics "sensor" sprites to get collision data, even for none physics based games as it gives you a little more to work with. If you use a "kinematic" sprite set up as a sensor as follows you can use it like any other sprite (not affected by gravity or collisions) but get more data from it.


I would use a User Defined Type for my player such as:


Then fill the array once at the start of my player update function:


Then you can loop through the sprites that currently collide with your player as follows:



Using AppGameKit V2 Tier 1
Scifictus
9
Years of Service
User Offline
Joined: 7th Feb 2015
Location:
Posted: 22nd Feb 2015 10:14
Thanks Baxslash - the GetSpriteFirstContact and GetSpriteNextContact were exactly what I was looking for - I guess I was looking in the wrong place as I assumed there would be such a command for sprite collisions without having to enable physics.

So looking at overall processing overhead, and assuming I will have around 150 sprite IDs in total, do you think that enabling physics on all of these just to be able to use the neater coding approach is still preferable? Or do you think that leaving physics off and just using my current for/next loops would actually be more efficient at run time?

Login to post a reply

Server time is: 2024-11-21 15:54:28
Your offset time is: 2024-11-21 15:54:28