Jump to content

Efficient drag and drop ?


B.Guyl
 Share

Recommended Posts

Hi !

I started with Phaser3 this weekend, I wanted to make a card game.
I'm struggling with the lack of documentation.

My current way to make a draggable object is to track the position of the `pointermove` event.

export class Card extends Phaser.GameObjects.Sprite {
  constructor (scene: Phaser.Scene, x: number, y: number, frame?: string) {
    super(scene, x, y, 'card', frame)
    this.setInteractive()
    this.on('pointerdown', () => this.dragOn())
    this.on('pointerup', () => this.dragOff())
  }

  private dragOn (): void {
    this.addListener('pointermove', (e: any) => {
      this.x = e.event.x
      this.y = e.event.y
    })
  }

  private dragOff (): void {
    this.removeAllListeners('pointermove')
  }
}

But I have an issue when I move my mouse too fast.
Is there a good way of doing this ?

Also, bonus question: Is there a list of available events ?

Link to comment
Share on other sites

Thank's !

Now my code look like this:


export class Card extends Phaser.GameObjects.Sprite {
  constructor (scene: Phaser.Scene, x: number, y: number, frame?: string) {
    super(scene, x, y, 'cards', 'back')
    this.setInteractive()
    scene.input.setDraggable(this)
    scene.input.on('drag', (pointer: any, gameObject: Card, dragX: number, dragY: number) => {
      gameObject.x = dragX
      gameObject.y = dragY
    })
  }
}

I probably should move the event listener on the scene level.

Is this working better because the `drag` event fire more events thant `pointermove` ?

 

Link to comment
Share on other sites

  • 10 months later...
  • 7 months later...
  • 9 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...