如何使用PhaserJS添加基本的世界碰撞?

hRd*_*der 6 javascript html5 phaser-framework

我正在使用PhaserJS库为HTML5开发一款新游戏,我遇到了一个问题.我正在使用P2物理引擎进行基本平台物理,我无法让世界边界碰撞工作.这是我的代码:

Game.js

function create() {
    game.world.setBounds(0, 0, 800, 300);
    game.physics.startSystem(Phaser.Physics.P2JS);
    game.physics.p2.restitution = 0.8;

    player.create(game);
    player.instance.body.collideWorldBounds = true;
}
Run Code Online (Sandbox Code Playgroud)

Player.js

Player.prototype.create = function(game) {
    this.instance = game.add.sprite(100, 100, 'player');
    game.physics.p2.enable(this.instance, Phaser.Physics.P2JS);

    this.cursors = game.input.keyboard.createCursorKeys();
};
Run Code Online (Sandbox Code Playgroud)

现在我的理解是我需要通过调用"game.world.setBounds(width,height)"来设置世界边界,然后通过调用"player.instance.body.collideWorldBounds = true;"来检查边界,但是玩家精灵仍然正在通过边界.任何帮助是极大的赞赏.谢谢!

编辑:我正在使用PhaserJS 2.0.7.

GDP*_*DP2 6

您可能希望更新到Phaser 2.1.1,因为它们看起来已修复此问题.

您可以从此示例中看到.

这是该示例的源代码:

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

function preload() {

    game.load.image('stars', 'assets/misc/starfield.jpg');
    game.load.spritesheet('ship', 'assets/sprites/humstar.png', 32, 32);

}

var ship;
var starfield;
var cursors;

function create() {

    starfield = game.add.tileSprite(0, 0, 800, 600, 'stars');

    game.physics.startSystem(Phaser.Physics.P2JS);

    game.physics.p2.restitution = 0.8;

    ship = game.add.sprite(200, 200, 'ship');
    ship.scale.set(2);
    ship.smoothed = false;
    ship.animations.add('fly', [0,1,2,3,4,5], 10, true);
    ship.play('fly');

    //  Create our physics body. A circle assigned the playerCollisionGroup
    game.physics.p2.enable(ship);

    ship.body.setCircle(28);

    //  This boolean controls if the player should collide with the world bounds or not
    ship.body.collideWorldBounds = true;

    cursors = game.input.keyboard.createCursorKeys();

}

function update() {

    ship.body.setZeroVelocity();

    if (cursors.left.isDown)
    {
        ship.body.moveLeft(200);
    }
    else if (cursors.right.isDown)
    {
        ship.body.moveRight(200);
    }

    if (cursors.up.isDown)
    {
        ship.body.moveUp(200);
    }
    else if (cursors.down.isDown)
    {
        ship.body.moveDown(200);
    }

}
Run Code Online (Sandbox Code Playgroud)