我正在使用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.collideWorldBounds = true;"来检查边界,但是玩家精灵仍然正在通过边界。任何帮助是极大的赞赏。谢谢!
编辑:我正在使用PhaserJS 2.0.7。答案 0 :(得分:6)
您可能想要更新到Phaser 2.1.1,因为它们看起来已修复此问题。
您可以从this example看到。
以下是该示例的源代码:
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);
}
}
答案 1 :(得分: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)