How to prevent a collision from happening multiple times in one event....?

function collectStar(event)

if event.phase == “began” then

if (event.object1.name == “star” and event.object2.name == “myPlayer”) then

display.remove(event.object1)

starsCollected = starsCollected + 1

print(“Player collected a star!”)

end

end

end

I noticed in the console that it’s printing “Player has collected a star” multiple (3X) times when the player grabs them. It’s also feeding the starsCollected an incorrect value. How do I prevent this from happening?

More importantly…I have a feeling that I’m not coding my game correctly for what I REALLY want it to do. I’d like to have a level select screen (which I already have the template for) and the “stars” on each level to be saved/stored so that it will show on top of the level button on the level select screen. Is there a better way to log the star counter? Maybe a tutorial somewhere on the web that explains how to structure your game? I read a couple threads and sites that talk about using tables (which I kind of understand them, but not how to use them in my case). I’ve just been going with what I know/learned thus far but like I said…I feel like I’m not doing it the right way (even though the game looks and plays nicely).

My apologies if I come off as a newb who posts too much lol. I’ve been a tech geek since I was 12, and always wanted to learn how to code (especially games). This is my passion. When I found Corona, I thought it was the perfect place to start…and so far I was right.

First, return true to indicate that the collision event was handled, otherwise it will propagate and fire again for other objects.

Second, if that doesn’t help, try putting in a counter which keeps a record of the items collected and won’t let them be collected again within half a second. This depends on how fast your game moves, of course.

I tried using return true, but that didn’t help. My game also moves pretty fast as well.

I think it’s because there’s 3 stars and they all have obj.name = star

If I were to give them all different obj.name’s, I’d have to make a collision function for each of them. Which leads me to believe there’s a better way of doing this.

That’s why I asked if anyone knew a better way of structuring things such as collecting stars.

Any advice?

There are already a couple of flags that get created by default for corona objects. I believe if you set isHitTestable to false, or set the alpha to 0, upon collision, it’ll keep one star object from registering several collision events.

If that doesn’t work out for whatever reason, you could just create your own flag on the star variable and use that in your collision listener.

Star.canCollect = true

if event.other1.canCollect == true then
event.other1.canCollect = false
end

Sorry for no formatting; I’m mobile.

Also, you seem to be using a global listener, maybe try just a local listener on the player. This should cause the event to only be fired once. Still return true though.

Also, you mentioned in the first post that the comment is being printed three times. You mention in your second post that there are three stars. Could this be part of the reason and that maybe it’s working better than you thought?

Can you boil your code down a bit more to show what is happening?

First, return true to indicate that the collision event was handled, otherwise it will propagate and fire again for other objects.

Second, if that doesn’t help, try putting in a counter which keeps a record of the items collected and won’t let them be collected again within half a second. This depends on how fast your game moves, of course.

I tried using return true, but that didn’t help. My game also moves pretty fast as well.

I think it’s because there’s 3 stars and they all have obj.name = star

If I were to give them all different obj.name’s, I’d have to make a collision function for each of them. Which leads me to believe there’s a better way of doing this.

That’s why I asked if anyone knew a better way of structuring things such as collecting stars.

Any advice?

There are already a couple of flags that get created by default for corona objects. I believe if you set isHitTestable to false, or set the alpha to 0, upon collision, it’ll keep one star object from registering several collision events.

If that doesn’t work out for whatever reason, you could just create your own flag on the star variable and use that in your collision listener.

Star.canCollect = true

if event.other1.canCollect == true then
event.other1.canCollect = false
end

Sorry for no formatting; I’m mobile.

Also, you seem to be using a global listener, maybe try just a local listener on the player. This should cause the event to only be fired once. Still return true though.

Also, you mentioned in the first post that the comment is being printed three times. You mention in your second post that there are three stars. Could this be part of the reason and that maybe it’s working better than you thought?

Can you boil your code down a bit more to show what is happening?