fedora Posted October 20, 2015 Share Posted October 20, 2015 I am in the process of creating a simple game in Phaser, but I am having difficulty with "collecting" objects. I have created a player and a single coin object. var player;var coin; I have them appearing on the screen:player = this.add.sprite(350, this.world.height - 150, 'villan');this.physics.p2.enable(player);player.body.setCircle(22); player.body.fixedRotation=true;player.body.mass = 4; coin = this.add.sprite(450, this.world.height - 450, 'booty');this.physics.p2.enable(coin);coin.body.setCircle(22); coin.body.fixedRotation=true;coin.body.mass = 4;Unfortunately, I have been unsuccessful in making it work so that when the player overlaps the coin- the coin disappears.update: function () { this.physics.arcade.collide(player,coin,this.collectCoin,null,this); collectCoin: function () {coin.kill();},Although game runs, when the player comes in contact with the coin it actually pushes it. Any guidance would be greatly appreciated. Link to comment Share on other sites More sharing options...
WombatTurkey Posted October 20, 2015 Share Posted October 20, 2015 switch coin and player in the parameter list maybe? this.physics.arcade.collide(coin, player,this.collectCoin,null,this); An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them, unless you are colliding Group vs. Sprite, in which case Sprite will always be the first parameter. Link to comment Share on other sites More sharing options...
chongdashu Posted October 20, 2015 Share Posted October 20, 2015 Are your variables global?Try changing to function collectCoin(player, coin) { ... } .Also, try adding debug renders to make sure the bodies are correct,function render() { game.debug.body(player) } Link to comment Share on other sites More sharing options...
Skeptron Posted October 20, 2015 Share Posted October 20, 2015 Use overlap and not collide : otherwise the player will collide with the coin and push it. That's the normal behaviour! Doc : http://phaser.io/docs/2.3/Phaser.Physics.Arcade.html#overlap Link to comment Share on other sites More sharing options...
fedora Posted October 21, 2015 Author Share Posted October 21, 2015 Unfortunately, I made these two changes and it still pushes the coin:this.physics.arcade.overlap(player,coin,this.collectCoin,null,this); collectCoin: function (player, coin) { coin.kill();}, Link to comment Share on other sites More sharing options...
DonFrag Posted October 21, 2015 Share Posted October 21, 2015 why do you use arcade collide if player and coin are using p2 physics? chongdashu 1 Link to comment Share on other sites More sharing options...
jmp909 Posted October 21, 2015 Share Posted October 21, 2015 DonFrag... drhayes and chongdashu 2 Link to comment Share on other sites More sharing options...
fedora Posted October 21, 2015 Author Share Posted October 21, 2015 In reading more, I think it is because I have elected to us create: function () { this.physics.startSystem(Phaser.Physics.P2JS); I continue to research, but if anyone has some suggestions on how to get overlap to work with this physics it would be greatly appreciated. Link to comment Share on other sites More sharing options...
fedora Posted October 21, 2015 Author Share Posted October 21, 2015 I just posted what you said Donfrag -- it was my lack of knowledge -- I am going to try to gain insight using this example http://test.xapient.net/phaser/overlap.html If you know of any others, it would be much appreciated. Link to comment Share on other sites More sharing options...
DonFrag Posted October 21, 2015 Share Posted October 21, 2015 well.the coin need to have a p2 body?if it does you can use postbroadphase to prevent collision event if it is not necessary you could use sprite.overlap method on the coin http://phaser.io/docs/2.4.4/Phaser.Sprite.html#overlap Link to comment Share on other sites More sharing options...
chongdashu Posted October 21, 2015 Share Posted October 21, 2015 If you are using p2 physics, you can't use game.physics.arcade to do the collision check. Instead, you should use the p2 collision system. Add he following after the creation of player and coin.var playerCollisionGroup = game.physics.p2.createCollisionGroup();var coinCollisionGroup = game.physics.p2.createCollisionGroup();player.body.setCollisionGroup(playerCollisionGroup);coin.body.setCollisionGroup(coinCollisionGroup);player.body.collides(coinCollisionGroup, collectCoin, this);coin.body.collides(playerCollisionGroup); Link to comment Share on other sites More sharing options...
fedora Posted October 21, 2015 Author Share Posted October 21, 2015 Newbie -- thanks, but I used your code, making a slight change (this.collectCoin):var playerCollisionGroup = game.physics.p2.createCollisionGroup();var coinCollisionGroup = game.physics.p2.createCollisionGroup();player.body.setCollisionGroup(playerCollisionGroup);coin.body.setCollisionGroup(coinCollisionGroup);player.body.collides(coinCollisionGroup, this.collectCoin, this);coin.body.collides(playerCollisionGroup);Without the change it would not run. After the change, it ran but --- It ran -- but two things did not happen -- first the coins fell from the sky and were not stopped by the ground and second even when they overlapped it did not call the function --- Link to comment Share on other sites More sharing options...
chongdashu Posted October 21, 2015 Share Posted October 21, 2015 first the coins fell from the sky and were not stopped by the ground if you need it to be stopped by the ground, you will need to add a collision group for the ground too (or use the world bounds) and second even when they overlapped it did not call the functionasAre you talking about the overlapping of the coin and the player, or the coin and the ground? Anyway, you also need to add the following line right after your physics system is initialized:game.physics.startSystem(Phaser.Physics.P2JS);game.physics.p2.setImpactEvents(true); // Note: Turn this onI replicated your system with this jsFiddle: https://jsfiddle.net/chongdashu/z1cd3g95/2/ It works just fine for me, so I would double check that the radius you are setting for your circles are correct (they might be a lot smaller than your sprite, for example). I turned on debug bodies in the fiddle so that you can see this clearly. Hope it helps! Link to comment Share on other sites More sharing options...
fedora Posted October 21, 2015 Author Share Posted October 21, 2015 chongdashu: Thank you. I will troubleshoot tonight and let you know. The jsFiddle is so useful to me to gain a better understanind so it is GREATLY appreciated. Link to comment Share on other sites More sharing options...
fedora Posted October 22, 2015 Author Share Posted October 22, 2015 chongdashu: Thank you. I got it to work. Link to comment Share on other sites More sharing options...
chongdashu Posted October 22, 2015 Share Posted October 22, 2015 If possible could you share the sprites you are using? Or simply state their dimensions? chongdashu 1 Link to comment Share on other sites More sharing options...
chongdashu Posted October 22, 2015 Share Posted October 22, 2015 Ah fantastic! I'm happy to hear that it worked out in the end Link to comment Share on other sites More sharing options...
Recommended Posts