Jump to content

Catch coin and increment variable


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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...