简体   繁体   中英

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

For example, if the sprite is in the middle of the screen and you press somewhere around the lower right of the screen and move 4 units to the left, the sprite will also move 4 units to the left with respect to its current position. So basically, input.x is not necessarily sprite.x. I hoe you can help. Thanks!

If it wasn't explained well, you can check the app Ballz Rush and see how the control works. Thank you very much!

Basically instead of making the sprite movable, you want to instead check to see if the active pointer is in the state you want to track (in this case, down).

One way to do this is in your update() check and see if the pointer is down, and if it is, move the sprite based upon what the pointer is doing.

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;
    }
}

The clone() method is available off of the Phaser.Point type , and creates a copy to make sure we're not keeping a reference to the active pointer's position, since we want a snapshot in time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM