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.

AppGameKit Classic Chat / Checking collision in an array

Author
Message
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 11th May 2012 06:55 Edited at: 11th May 2012 06:56
I have an array that makes and drops some pucks. If the pucks collide with the bar or go past 85% of Y then they get deleted. I have my collision check in the main loop. Everything works great until the sprite gets deleted, on the next loop I get an error saying that it can't find the sprite. Well I hope it can't I deleted it. I am not sure on how to get around the error. Also I would prefer not to use if and elseif statements all the time but I am drawing a blank on how to change it. Any help is greatly appreciated.

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 11th May 2012 08:46 Edited at: 11th May 2012 08:47
Quote: "Well I hope it can't I deleted it. "

That's the problem, it expects the sprite to exist. It can not perform a collision check on a sprite that doesn't exist and so throws you an error. A quick fix would be to simply check if the sprite exists before checking collision. E.g



Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 11th May 2012 16:39
Thanks
Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 12th May 2012 05:36
It doesn't work. If I do the check outside the For i = 1 to 5 it never triggers the collision. If I put it inside the for i = 1 to 5 then I get the same error that the sprite doesn't exist.
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 12th May 2012 05:47
You need to nest the if statements and it must be inside the for loop because you want to check for the existence for each puck, like this:



Remember, just about all of the sprite commands will return an error if the sprite doesn't exist. So if you're constantly creating and/or deleting sprites, it's a good idea to throw in a few existence checks.

Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 12th May 2012 06:08
I must be missing something. This is the code for the collision and it produces the error.

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 12th May 2012 06:28 Edited at: 12th May 2012 06:29
You should try to indent your code mate, it makes finding bugs a lot easier.

The problem is here:


It is possible that the sprite is deleted by the time it is checked against here, despite us checking its existence earlier. You have two options, one is to check for the existence again:



or make it another elseif statement and add it on to the list of elseifs you have there:



Kobaltic
12
Years of Service
User Offline
Joined: 24th Jan 2012
Location: PA, USA
Posted: 12th May 2012 06:39
Thanks man that fixed it. Yeah I will go back and indent the code.

Login to post a reply

Server time is: 2024-11-23 11:09:26
Your offset time is: 2024-11-23 11:09:26