我正在使用HTML5的PhaserJS庫開發一款新遊戲,並且遇到了我遇到的問題。我使用P2物理引擎進行基礎平臺物理學,並且無法讓世界範圍的碰撞起作用。這裏是我的代碼:如何使用PhaserJS添加基本的世界碰撞?
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設置世界界限(寬度,高度)「,然後通過調用」player.instance.body.collideWorldBounds = true;「來檢查邊界,但玩家精靈仍然會穿過邊界。任何幫助是極大的讚賞。謝謝!
編輯:我使用PhaserJS 2.0.7。
這可能是2.0.7版本可能會導致我幾個問題,我沒有意識到一個新版本,老實說。我今晚會嘗試一下,並告訴你它是否適合我。感謝您的回答。 – hRdCoder 2014-09-23 16:13:04
好的,太好了。希望這對你有用。 – GDP2 2014-09-23 17:44:30
它的工作!非常感謝你@Ylluminarious。 :) – hRdCoder 2014-09-24 13:51:10