拖动一个精灵来移动另一个精灵
Drag a sprite to move another sprite
我是 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 精灵的位置只移动到我开始拖动的位置。它只移动到点击事件。
需要覆盖 updateDrag
函数。
controller.input.updateDrag = function(pointer) {
player.x = pointer.x;
// call the generic implementation:
Phaser.InputHandler.prototype.updateDrag.call(this,pointer);
}
对于任何对如何使用来自另一个 sprite 的拖动输入来移动 sprite 而不实际移动被拖动的 sprite 感兴趣的人(就像我发现这个问题时一样):
下面是一个使用来自背景 sprite 的拖动输入来移动玩家 sprite 的示例:
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 的新手,所以我不确定是否有更好的方法,但它对我有用,所以我想分享一下!
我是 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 精灵的位置只移动到我开始拖动的位置。它只移动到点击事件。
需要覆盖 updateDrag
函数。
controller.input.updateDrag = function(pointer) {
player.x = pointer.x;
// call the generic implementation:
Phaser.InputHandler.prototype.updateDrag.call(this,pointer);
}
对于任何对如何使用来自另一个 sprite 的拖动输入来移动 sprite 而不实际移动被拖动的 sprite 感兴趣的人(就像我发现这个问题时一样):
下面是一个使用来自背景 sprite 的拖动输入来移动玩家 sprite 的示例:
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 的新手,所以我不确定是否有更好的方法,但它对我有用,所以我想分享一下!