Jump to content

Passing the cursor object to the Phaser.Sprite class


mike perry
 Share

Recommended Posts

How to pass the cursor object to a method that belongs to a class that inherits the Phaser.Sprite class?

I have a class that inherits from Phaser.State in which I create a cursor object and pass it to the update method from another class.

class Play extends Phaser.State {
  create() {
    this.physics.startSystem(Phaser.Physics.ARCADE)

    ...

    this.player = new Player({
      game: this.game,
      x: 32,
      y: this.world.height - 150,
      asset: 'dude'
    })
    this.game.add.existing(this.player)
  }

  update() {
    const cursors = this.input.keyboard.createCursorKeys()
    this.player.update(cursors)
  }
}

Player class - problem occurs in the if condition:

class Player extends Phaser.Sprite {
  constructor({ game, x, y, asset }) {
    super(game, x, y, asset)

    this.game.physics.arcade.enable(this)

    this.body.bounce.y = 0.2
    this.body.gravity.y = 300
    this.body.collideWorldBounds = true

    this.animations.add('left', [0, 1, 2, 3], 10, true)
    this.animations.add('right', [5, 6, 7, 8], 10, true)
  }

  update(cursors) {
    this.body.velocity.x = 0

    if (cursors.left.isDown) {
      this.body.velocity.x = -150
      this.animations.play('left')
    }
  }
}

Error message: "TypeError t is undefined"

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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