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

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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.