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

时间:2018-07-12 03:05:39

标签: javascript phaser-framework

例如,如果子画面位于屏幕中间,并且在屏幕右下角按某个位置并向左移动4个单位,则子画面也将相对于其当前位置向左移动4个单位位置。因此,基本上,input.x不一定是sprite.x。我希望你能提供帮助。谢谢!

如果解释不正确,您可以检查应用程序Ballz Rush并查看控件的工作方式。非常感谢你!

1 个答案:

答案 0 :(得分:0)

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

一种方法是在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,并创建一个副本以确保我们没有保留对活动指针位置的引用,因为我们需要及时的快照。