Sign in to follow this  
mike perry

Passing the cursor object to the Phaser.Sprite class

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"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.