我是Phaser的新手,并且在查看文档以解决我的问题时没有运气。
我正在创建一个移动游戏,它将使用触摸(拖动)来移动玩家精灵。我希望用户能够在移动时看到玩家精灵,所以我想添加一个精灵,仅用于控制玩家精灵。
玩家精灵只能向左或向右移动。 这是我到目前为止创建的代码,但它不能完成我想要的操作
var controller = game.add.sprite(0, scaleHeight(1150), cbox);
game.physics.enable(controller, Phaser.Physics.ARCADE);
controller.inputEnabled = true;
controller.input.enableDrag(true);
controller.input.allowHorizontalDrag = true;
controller.input.allowVerticalDrag = false;
controller.body.immovable = true;
controller.body.moves = false;
controller.events.onDragStart.add(doSomething, this);
第一个问题是当我拖动时Controller
精灵正在移动。第二个问题是Player精灵的位置只移动到我开始拖动的位置。它只会移动到点击事件。
答案 0 :(得分:0)
需要覆盖updateDrag
功能。
controller.input.updateDrag = function(pointer) {
player.x = pointer.x;
// call the generic implementation:
Phaser.InputHandler.prototype.updateDrag.call(this,pointer);
}
答案 1 :(得分:0)
对于任何有兴趣如何使用来自另一个精灵的拖动输入移动精灵而不实际移动拖动的精灵的人(就像我在找到这个问题时那样):
以下是使用背景精灵中的拖动输入来移动玩家精灵的示例:
var self = this;
this.background = this.add.sprite(0, 0, 'background');
this.background.inputEnabled = true;
this.background.input.enableDrag();
this.player = this.add.sprite(this.world.centerX, this.world.centerY, 'player');
this.player.anchor.setTo(0.5);
this.background.input.startDrag = function(pointer) {
pointer.playerStart = new Phaser.Point(self.player.x, self.player.y);
Phaser.InputHandler.prototype.startDrag.call(this, pointer);
};
this.background.input.updateDrag = function(pointer) {
self.player.x = pointer.playerStart.x - pointer.positionDown.x + pointer.x;
self.player.y = pointer.playerStart.y - pointer.positionDown.y + pointer.y;
};
如果你想让玩家保持在世界的范围内:
var self = this;
this.background = this.add.sprite(0, 0, 'background');
this.background.inputEnabled = true;
this.background.input.enableDrag();
this.player = this.add.sprite(this.world.centerX, this.world.centerY, 'player');
this.player.anchor.setTo(0.5);
this.background.input.startDrag = function(pointer) {
pointer.playerStart = new Phaser.Point(self.player.x, self.player.y);
Phaser.InputHandler.prototype.startDrag.call(this, pointer);
};
this.background.input.updateDrag = function(pointer) {
self.player.x = pointer.playerStart.x - pointer.positionDown.x + pointer.x;
self.player.y = pointer.playerStart.y - pointer.positionDown.y + pointer.y;
if (self.player.x > self.world.width){
self.player.x = pointer.playerStart.x = self.world.width;
pointer.positionDown.x = pointer.x;
}
if (self.player.y > self.world.height){
self.player.y = pointer.playerStart.y = self.world.height;
pointer.positionDown.y = pointer.y;
}
if (self.player.x < 0){
self.player.x = pointer.playerStart.x = 0;
pointer.positionDown.x = pointer.x;
}
if (self.player.y < 0){
self.player.y = pointer.playerStart.y = 0;
pointer.positionDown.y = pointer.y;
}
};
我是Phaser的新手,所以我不确定是否有更好的方法可以做到这一点,但这对我有用,所以我想我会分享!