Breno Pinheiro Posted April 19, 2016 Share Posted April 19, 2016 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 More sharing options...
Heppell08 Posted April 20, 2016 Share Posted April 20, 2016 moedaspegas+=1; Link to comment Share on other sites More sharing options...
o0Corps0o Posted April 20, 2016 Share Posted April 20, 2016 would it be that you need moeda.kill(); ? Link to comment Share on other sites More sharing options...
Breno Pinheiro Posted April 20, 2016 Author Share Posted April 20, 2016 The P2 Physics don´t have a method called kill. If I use the moeda.destroy(), the body of the coin is destroyed, but the sprite keeps showed. Link to comment Share on other sites More sharing options...
Cudabear Posted April 20, 2016 Share Posted April 20, 2016 I believe you need to use moda.sprite.destroy(); That should get rid of both the sprite and it's P2 body. Otherwise, if you want to reuse the sprite later by reviving it, you could also wrap the code in an if(moda.sprite.alive) check. Heppell08 1 Link to comment Share on other sites More sharing options...
Breno Pinheiro Posted April 22, 2016 Author Share Posted April 22, 2016 moeda.sprite.alive works if I use the phaser in a browser game, but If I use in a cordova app, an error is showed saying that the propriety don't exist. Link to comment Share on other sites More sharing options...
Cudabear Posted April 22, 2016 Share Posted April 22, 2016 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 More sharing options...
Recommended Posts