6

我正在使用适用于 HTML5 的 PhaserJS 库开发一款新游戏,但遇到了一个问题,我不知所措。我将 P2 物理引擎用于基本平台物理,但我无法让世界边界碰撞工作。这是我的代码:

游戏.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;
}

播放器.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();
};

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

编辑:我正在使用 PhaserJS 2.0.7。

4

2 回答 2

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

}
于 2014-09-22T15:07:34.843 回答
3

在我的游戏中,我不得不调用p2.setBoundsToWorld更新 p2 物理边界以匹配世界边界:

function create() {
    game.world.setBounds(0, 0, 800, 300);
    game.physics.startSystem(Phaser.Physics.P2JS);
    game.physics.p2.setBoundsToWorld(true, true, true, true, false);

签名如下:

setBoundsToWorld: function (left, right, top, bottom, setCollisionGroup)
于 2014-09-22T13:50:06.093 回答