javascript - 在初始检测后取消碰撞,在 js 中使用相位器

标签 javascript phaser-framework

当 2 个 sprite 发生碰撞时,我想将其计为 1 条生命损失,然后取消碰撞以使 sprite 彼此擦肩而过。我不希望因为相同的 2 个 Sprite 相互擦肩而过而失去多条生命。

有什么想法可以在减去 1 条生命后取消联系吗?

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

最佳答案

是否可以选择在碰撞后立即销毁粒子?如果是,就这样做,只需像这样编辑 change() 函数:

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

第二个参数恰好是粒子本身,第一个是发射器。

关于javascript - 在初始检测后取消碰撞,在 js 中使用相位器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30854960/

相关文章:

javascript - 显示日期总是没有秒是行不通的

javascript - Phaser 3 中的 "this"指的是什么?

javascript - 我的 Phaser.js 游戏中的音频无法在 Android 设备上播放

javascript - 如何在 Phaser 中启用跳跃 "just before"玩家接触地面?

javascript - 更改输入文本的颜色

javascript - 如何根据其祖 parent 的子值从列表中选择一个元素

javascript - Cordova:设备就绪不显示

javascript - Phaser 3 俄罗斯方 block 克隆,但方 block 不会落地

Javascript 四舍五入到小数点后两位

javascript - 是否可以在浏览器中拦截非ajax请求?