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

Javascript 如何使用PhaserJS添加基本世界碰撞?,javascript,html,phaser-framework,Javascript,Html,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;

我正在使用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;
}
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();
};
现在我的理解是,我需要通过调用“game.world.setBounds(width,height)”来设置世界边界,然后通过调用“player.instance.body.CollizeWorldBounds=true;”来检查边界,但玩家精灵仍然在正确地通过边界。非常感谢您的帮助。谢谢


编辑:我正在使用PhaserJS 2.0.7。

在我的游戏中,我必须调用
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)

您可能需要更新到Phaser,因为看起来他们已经解决了这个问题

你可以从照片上看到这一点

下面是该示例的源代码:

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

}

实际上,我不知道那种方法。今晚我要试一试,看看有没有结果。谢谢你的回答。那可能是版本2.0.7给我带来了一些问题,老实说,我不知道有更新的版本。今晚我会试试这个,如果对我有效,我会告诉你的。谢谢你的回答。