我的编码出了什么问题?它没有碰撞

时间:2015-10-02 09:29:22

标签: javascript phaser-framework

这是代码,它是100%运行的。我正在尝试研究答案,但我无法找到/解决它。两个精灵不会相互碰撞。我希望碰撞功能运行。

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

function preload(){

game.load.spritesheet('mummy', 'metalslug_mummy37x45.png', 37, 45, 18);
game.load.spritesheet('sprite', 'sprite.png', 39, 40);
game.load.image('background', 'cemetery.jpg');

}
var sprites;
var rip = 0;
var s;

function create(){

    game.add.sprite(0, 0, 'background');

    sprites = game.add.group();
    game.time.events.loop(80, createSprite, this);
    game.physics.enable(sprites, Phaser.Physics.ARCADE);

    s = game.add.sprite(game.world.centerX, game.world.centerY, 'sprite');
    s.anchor.setTo(0.5, 0.5);
    s.scale.setTo(2, 2);

    s.animations.add('run');
    s.animations.play('run', 10, true);
    game.physics.enable(s, Phaser.Physics.ARCADE);



}

function createSprite(){

    var mummy = sprites.create(0, game.world.randomY, 'mummy');

    mummy.animations.add('walk');

    mummy.play('walk', 10, true);

}

function update(){

    sprites.setAll('x', 10, true, true, 1);

    sprites.forEach(checkSprite, this, true);

    game.physics.arcade.collide(s, sprites, collisionHandler, null, this);

    if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
    {
        s.x -= 5;
    }
    else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
    {
        s.x += 5;
    }

    if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
    {
        s.y -= 5;
    }
    else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
    {
        s.y += 5;
    }


}

function collisionHandler(obj1,obj2){

    game.stage.backgroundColor = '#992d2d';

}

function checkSprite(sprite){

    try {
        if (sprite.x > game.width)
        {
            rip++;
            sprites.remove(sprite, true);
        }
    }
    catch (e)
    {
        console.log(sprite);
    }

}

function render(){

    game.debug.text("Score: " + rip, 32, 64);

}

1 个答案:

答案 0 :(得分:0)

这是问题 -

`  sprites.setAll('x', 10, true, true, 1);

   sprites.forEach(checkSprite, this, true);`

你应该在创建功能而不是更新功能中写下这些功能,并确保你调用 game.physics.arcade.collide(// ..... //); 在更新功能的开头。

相关问题