Cancel collision after initial detection, using phaser in js

1.2k views Asked by At

When 2 sprites collide I'd like to count that as 1 life lost, and then cancel the collision so the sprites pass over one another. I don't want multiple lives to be lost as the same 2 sprites pass over each other.

Any ideas how I can cancel the contact after I've subsracted 1 life?

http://jsfiddle.net/bobbyrne01/44zmvm8z/

javascript ..

var player,
emitter,
lives = 5;

var game = new Phaser.Game(
800,
600,
Phaser.CANVAS,
    'Game', {
    preload: preload,
    create: create,
    update: update,
    render: render
});

function preload() {
    game.load.image('missile', 'http://images.apple.com/v/iphone-5s/a/images/buystrip_retail_icon.png');
    game.load.image('player', 'http://38.media.tumblr.com/avatar_0714f87e9e76_128.png');
}

function create() {

    game.physics.startSystem(Phaser.Physics.ARCADE);
    game.physics.arcade.gravity.y = 300;
    game.stage.backgroundColor = '#000';
    game.scale.fullScreenScaleMode = Phaser.ScaleManager.SHOW_ALL; // Maintain aspect ratio

    player = game.add.sprite(game.world.width / 2, game.world.height / 2, 'player');
    player.scale.setTo(0.5, 0.5);
    game.physics.arcade.enable(player);
    player.body.allowGravity = false;

    emitter = game.add.emitter(0, 100, 100);
    emitter.makeParticles('missile');
    emitter.gravity = 200;
    emitter.width = 500;
    emitter.x = game.world.width / 2;
    emitter.y = -300;
    emitter.minRotation = 0;
    emitter.maxRotation = 0;
    emitter.setScale(0.1, 0.5, 0.1, 0.5, 6000, Phaser.Easing.Quintic.Out);
    emitter.start(false, 2000, 500);
}

function update() {

    game.physics.arcade.collide(player, emitter, chec, change, this);

    if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
        player.x -= 4;

    } else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
        player.x += 4;
    }
}

function chec() {}

function change() {
    lives--;
    return false;
}

function render() {
    game.debug.text('Lives: ' + lives, 2, 28, "#00ff00");
}
1

There are 1 answers

0
Kamen Minkov On BEST ANSWER

Is it optional to destroy the particle just after the collision? If yes, do it this way, just edit the change() function like this:

function change(a, b) {
    b.destroy();
    lives--;
    return false;
}

The second parameter happens to be the particle itself, the first one is the emitter.