Phaser 2.8.1 如何从屏幕任意位置拖动精灵?

Phaser 2.8.1 How to drag sprite from anywhere in the screen?

例如,如果精灵在屏幕中间,你按下屏幕右下角附近的某处并向左移动4个单位,则精灵也会相对于屏幕向左移动4个单位它的当前位置。所以基本上,input.x 不一定是 sprite.x。我希望你能帮忙。谢谢!

如果没有解释清楚,你可以查看应用程序Ballz Rush,看看控件是如何工作的。非常感谢!

基本上不是让精灵可移动,而是要检查活动指针是否处于您要跟踪的状态(在本例中为向下)。

一种方法是 update() 检查指针是否向下,如果是,则根据指针的动作移动精灵。

function update() {
    // This depends upon pointerOrigin (Phaser.Point) being defined. You'll also need to update 'player' to match your specific needs.
    if (this.game.input.activePointer.isDown) {
        if (this.pointerOrigin) {
            this.player.position.x += this.game.input.activePointer.position.x - this.pointerOrigin.x;
            this.player.position.y += this.game.input.activePointer.position.y - this.pointerOrigin.y;
        }
        // Keep track of where the pointer has been so we can use it for the next update cycle.
        this.pointerOrigin = this.game.input.activePointer.position.clone();
    }
    else {
        this.pointerOrigin = null;
    }
}

clone() method is available off of the Phaser.Point type,并创建一个副本以确保我们没有保留对活动指针位置的引用,因为我们需要及时的快照。