Jump to content

Catch coin and increment variable


Breno Pinheiro
 Share

Recommended Posts

Hello, I am trying to create a game, using P2 Physics, that the user can catch coins. The problem is that when the user catch a coin, I need to increment a variable. If I use a code like this

        astronauta.body.collides(moedaCollisionGroup, function (astronauta, moeda) {

            moeda.sprite.kill();
            moedaspegas++;           
            exit;
                
        }, this);

The sprite of coin is hided, but the variable moedaspegas keeps being incremented. How can I do to the variable moedaspegas is incremented once? 

Link to comment
Share on other sites

P2 has a quirk that sometimes you collide with a body that doesn't have a sprite, or doesn't know what it's sprite is.  I don't really know why this is, and the documentation says "if the p2 body was created without a sprite" but it still happens sometimes even if I know all the bodies are connected to sprites

That said, add a check to see if the sprite exists and it should fix this problem.  

 

if(moeda.sprite && moeda.sprite.alive)

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...