Reputation: 8802
I am building a simple infinite runner game with Phaser.io.
I have a rider and the rider object keeps moving right.
Several obstacles come in its way. I want to detect when a collision takes place. But I do not seem to be able to detect any collision or overlap. Please help!
Some of my code snippets are below -
function create () {
//obstacles initialization
obstacles = game.add.group();
obstacles.enableBody = true;
//player initialization
player = game.add.sprite(10, 250, 'tuktuk'); game.physics.arcade.enable(player);
player.body.bounce.y = 0.2;
player.body.gravity.y = 800;
player.body.bounce.x = 0.2;
player.body.collideWorldBounds = true;
player.animations.add('right', [0,1,2], 20, true);
create_random_obstacle();
game.physics.arcade.enable(obstacles);
game.world.bringToTop(player);
game.physics.arcade.overlap(obstacles, player, after_collision, null, null);
}
Upvotes: 1
Views: 830
Reputation: 1604
Along with B. Naeem's answer:
game.physics.arcade.overlap(obstacles, player, after_collision, null, null);
Should be in update(), as it has to check for the overlap at every frame.
Upvotes: 2
Reputation:
You need to enable physics first before you can check for any collisions between sprites. You can do that by
game.physics.startSystem(Phaser.Physics.ARCADE);
You should also enable the physics for the player sprite
game.physics.arcade.enable(player);
Upvotes: 2
Reputation: 3058
I think you also need to enable physics on the player sprite.
game.physics.arcade.enable(player);
Also, in my game I set the first parameter of overlap
as the sprite, and the second parameter is the group. Although it shouldn't matter according to the Phaser docs, but you could try just to be sure.
Upvotes: 1