Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Trouble with this.physics.arcade.collide


fedora
 Share

Recommended Posts

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

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

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

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

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

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 functionas

Are 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 on

I 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

 Share

  • Recently Browsing   0 members

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