geeky_monster
geeky_monster

Reputation: 8802

Not able to detect collision with Phaser.IO

I am building a simple infinite runner game with Phaser.io. enter image description here 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

Answers (3)

Sawyer05
Sawyer05

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

user7266839
user7266839

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

BdR
BdR

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

Related Questions