Jump to content

Problem with collide method from physics arcade in different versions of Phaser


AmAuron
 Share

Recommended Posts

​Hi all,

sorry if i'm doing some kind of rookie mistake, but i started HTML5 programming at only 1 day and half and one problem i found was that the collide method from game.physics.arcade works differently in different versions of Phaser.

 

The same piece of code:

 

 

create: function(){    // Sprite Loading before this    this.game.physics.startSystem(Phaser.Physics.ARCADE);    this.game.physics.arcade.gravity.y = 400;    this.game.physics.arcade.enableBody(this.ground);    this.ground.body.allowGravity = false;    this.ground.body.immovable = true;     this.game.physics.arcade.enableBody(this.player);    this.player.body.collideWorldBounds = true;    this.player.body.bounce.set(0.25);  },  update: function() {    this.game.physics.arcade.collide(this.player.body, this.ground.body);  },  

 

in phaser version 2.0.3 the player collides with the ground and separates like its supposed to.

 

in the new version of phaser, 2.3.0 the player ignores the ground and only stops at the World bounds.

 

Why is this happening?

 

And Thank you for the patiente to help a new guy :)

Link to comment
Share on other sites

Hi there rich, first of all thank you for your quick response...i already made the alteration you sugested and even added a callback function to know if the collision is happening and i still have the same problem, what did i do wrong this time? 

Here's an updated version of my code:

create: function(){     // Sprite Loading before this     this.game.physics.startSystem(Phaser.Physics.ARCADE);    this.game.physics.arcade.gravity.y = 400;      this.game.physics.arcade.enableBody(this.ground);    this.ground.body.allowGravity = false;    this.ground.body.immovable = true;      this.game.physics.arcade.enableBody(this.player);    this.player.body.collideWorldBounds = true;    this.player.body.bounce.set(0.25);  },  update: function() {     this.game.physics.arcade.collide(this.player, this.ground,this.collisionHandler, null, this);  },   collisionHandler: function(){     console.log('YAYYYYYYYYY');     this.player.body.bounce.y=0.25;  }

still having the same problem with phaser 2.0.3 it still works fine :x the new version has the same problem, never while the game is runing i get the "YAYYYYYYYYY"

Thank you for your patience and hope i'm not making another rookie mistake :)

Link to comment
Share on other sites

this.game.physics.arcade.enable(this.ground);
It should be 'enable', not 'enableBody' or it'll skip an important part of the set-up process.
Again thank you for the quick answer, and i'm sorry for taking so much of your time, 

did that new change to my code, and for some reason, still not getting the collision :(

here's the new updated code:

create: function(){     // Sprite Loading before this     this.game.physics.startSystem(Phaser.Physics.ARCADE);    this.game.physics.arcade.gravity.y = 400;      this.game.physics.arcade.enable(this.ground);    this.ground.body.allowGravity = false;    this.ground.body.immovable = true;      this.game.physics.arcade.enable(this.player);    this.player.body.collideWorldBounds = true;    this.player.body.bounce.set(0.25);  },  update: function() {     this.game.physics.arcade.collide(this.player, this.ground,this.collisionHandler, null, this);  },   collisionHandler: function(){     console.log('YAYYYYYYYYY');     this.player.body.velocity.y=-200;  }
Again thank you for the patience and time :)
Link to comment
Share on other sites

Where do you define your two objects? (this.player and this.ground).What types are they?

I define them on the preload...so the player is

this.load.image('ground', 'assets/images/ground.png'); this.load.spritesheet('player', 'assets/images/runningman.png', 229, 296, 4);

once my preload is done in the game file i only need to add the animations that is:

this.ground = this.game.add.tileSprite(0, this.game.height - 73, this.game.width, 73, 'ground');this.ground.autoScroll(-400,0);this.player = this.add.sprite(200, this.game.height/2, 'player');this.player.anchor.setTo(0.5);this.player.scale.setTo(0.3);this.player.animations.add('fly', [0,1,2,3,2,1]);this.player.animations.play('fly',8, true);

I didn't think it would be significant for the problem so that's why i didn't mention it.

 

so in the game file the full code is:

create: function(){this.ground = this.game.add.tileSprite(0, this.game.height - 73, this.game.width, 73, 'ground');this.ground.autoScroll(-400,0);this.player = this.add.sprite(200, this.game.height/2, 'player');this.player.anchor.setTo(0.5);this.player.scale.setTo(0.3);this.player.animations.add('fly', [0,1,2,3,2,1]);this.player.animations.play('fly',8, true);this.game.physics.startSystem(Phaser.Physics.ARCADE);this.game.physics.arcade.gravity.y = 400;this.game.physics.arcade.enable(this.ground);this.ground.body.allowGravity = false;this.ground.body.immovable = true;this.game.physics.arcade.enable(this.player);this.player.body.collideWorldBounds = true;this.player.body.bounce.set(0.25);},update: function() {this.game.physics.arcade.collide(this.player, this.ground,this.collisionHandler, null, this);},collisionHandler: function(){console.log('YAYYYYYYYYY');this.player.body.velocity.y=-200;}

mypreloader file is something like this:

preload: function(){this.load.image('ground', 'assets/images/ground.png');this.load.spritesheet('player', 'assets/images/runningman.png', 229, 296, 4);this.load.onLoadComplete.add(this.onLoadComplete, this);},create: function(){},update: function(){if(this.ready==true){this.state.start('Game.js');}},onLoadComplete: function(){this.ready = true;}};

Thank you for the patience for solving my issue, and again thank you for your time :)

Link to comment
Share on other sites

That's why - in phaser 2.3.0 TileSprites have a bug where they don't have their physics type set, stopping them from working. This is already fixed in the dev branch of Phaser, but in the meantime you could simply do this:

this.ground.physicsType = Phaser.SPRITE;

Before you enable physics on it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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