raoul Posted April 22, 2016 Share Posted April 22, 2016 Hello all, I'm a rookie with phaser i try just to collide two sprite together in the create part this.game.majMush = this.game.add.sprite(mMm.x, mMm.y, mMm.name); this.game.girlMush = this.game.add.sprite(mMm2.x, mMm2.y, mMm2.name); this.game.physics.arcade.enable( this.game.majMush); this.game.physics.arcade.enable( this.game.girlMush); this.game.majMush.body.collideWorldBounds = true; this.game.majMush.body.enable = true; this.game.girlMush.body.enable = true; in the update part var test = this.game.physics.arcade.collide(this.game.majMush, this.game.girlMush); console.log(test); i push a render to debug Game.prototype.render = function() { //console.log(majMush.x); this.game.debug.bodyInfo(this.game.majMush,32,32); this.game.debug.body(this.game.majMush); this.game.debug.body(this.game.girlMush); } i've checked my two object are Phaser.sprite, but impossible to both to collide.... damn i don't understand Link to comment Share on other sites More sharing options...
3ddy Posted April 22, 2016 Share Posted April 22, 2016 Hello, I'm also a rookie - I'm not sure if you enabled physics correctly - I was doing it using game.physics.enable(this.object, Phaser.Physics.ARCADE); in update function I would use something like that, without assigning it to some variable: game.physics.arcade.collide(this.obj1, this.obj2, function(obj1, obj2){ //handle collision if needed }, null, this); As mentioned earlier - I'm a beginner with physics so it may not work correctly, but I wanted to try to help you Link to comment Share on other sites More sharing options...
raoul Posted April 22, 2016 Author Share Posted April 22, 2016 hi 3ddy thanks for reply i try with u method with no success this.game.physics.enable(this.game.majMush, Phaser.Physics.ARCADE); this.game.physics.enable(this.game.girlMush, Phaser.Physics.ARCADE); when i watch the api enable(object, children) collide(object1, object2, collideCallback, processCallback, callbackContext) if i perform this test i can retrieve the correct axis (and perform this beautifful story between two mushroom) else if (this.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { this.game.majMush.body.x += 4; if( this.game.majMush.body.x >= this.game.girlMush.body.x){ text.text = "hello mushroom ? "; }; } Link to comment Share on other sites More sharing options...
Arcanorum Posted April 22, 2016 Share Posted April 22, 2016 Make sure you have the started the physics system. game.physics.startSystem(Phaser.Physics.ARCADE); Are there any errors in your browsers dev console? Also, why are you doing 'this.game.blahblah...'? It looks like a misunderstanding of how scope works. Link to comment Share on other sites More sharing options...
raoul Posted April 22, 2016 Author Share Posted April 22, 2016 i've started the engine (i think) in the create method intersect return true when i try to debug (inside phaser) -> this.intersects(body1, body2) true the whole code .. maybie its a scope problem function Game( ) { } var map; var layer; var cursors; var sprite; var majMush; var girlMush; var text; var mMm; var mMm2; Game.prototype.create = function () { function magicMushroom (x, y, name){ this.x = x, this.y = y, this.name= name }; function collisionHandler (obj1, obj2) { text.text = "coucou champignon bleu !"; }; this.game.physics.startSystem(Phaser.Physics.ARCADE); this.game.input.onDown.add(this.onInputDown, this); // this.map = this.add.tilemap('forest'); //lien entre l'image et le nom du tile set //this.map.addTilesetImage('forest_tiles', 'foresttiles'); map = this.game.add.tilemap('desert'); map.addTilesetImage('Desert', 'tiles'); layer = map.createLayer('Ground'); // this.layer = this.map.createLayer('front'); //crée le premier champignon text = this.add.text(0, 0, "test", { font: "25px Arial", fill: "#ff0044", align: "center" }); mMm = new magicMushroom(150, 200, 'mushroom'); mMm2 = new magicMushroom(400, 100, 'mushroom2'); this.game.majMush = this.game.add.sprite(mMm.x, mMm.y, mMm.name); this.game.girlMush = this.game.add.sprite(mMm2.x, mMm2.y, mMm2.name); //this.game.physics.arcade.enable( this.game.majMush); //this.game.physics.arcade.enable( this.game.girlMush); this.game.physics.enable(this.game.majMush, Phaser.Physics.ARCADE); this.game.physics.enable(this.game.girlMush, Phaser.Physics.ARCADE); this.game.majMush.body.collideWorldBounds = true; this.game.majMush.body.enable = true; this.game.girlMush.body.enable = true; }; Game.prototype.update = function () { //render(majMush, girlMush); var test = this.game.physics.arcade.collide(this.game.majMush, this.game.girlMush); console.log(test); if (this.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { this.game.majMush.x -= 4; } else if (this.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { this.game.majMush.body.x += 4; if( this.game.majMush.body.x >= this.game.girlMush.body.x){ text.text = "alors champignon ça va ? "; }; } if (this.input.keyboard.isDown(Phaser.Keyboard.UP)) { this.game.majMush.y -= 4; } else if (this.input.keyboard.isDown(Phaser.Keyboard.DOWN)) { this.game.majMush.y += 4; } }; Game.prototype.render = function() { //console.log(majMush.x); this.game.debug.bodyInfo(this.game.majMush,32,32); this.game.debug.body(this.game.majMush); this.game.debug.body(this.game.girlMush); } Game.prototype.onInputDown = function () { // this.game.state.start('menu'); }; module.exports = Game; Link to comment Share on other sites More sharing options...
Recommended Posts