Search code examples
collision-detectiongame-physicsphaser-framework

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);    
}

Solution

  • 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.