Jump to content

Moving a custom class that extends a Sprite


Recommended Posts

I haven't been able to figure this out. I have a "Player" class that extends Sprite:

export default class Player extends Phaser.GameObjects.Sprite{
   constructor(scene, x, y, config) {
    super(scene, x, y, config.key);

    this.scene = scene;
    this.lastAnim = null;
    this.vel = 200;


    const { LEFT, RIGHT, UP, DOWN } = Phaser.Input.Keyboard.KeyCodes;
    this.keys = this.scene.input.keyboard.addKeys({
      left: LEFT,
      right: RIGHT,
      up: UP,
      down: DOWN,

  preUpdate (time, delta) {
    super.preUpdate(time, delta);

    const keys = this.keys;
    let animationName = 'player-idle';

    if (keys.left.isDown) {
    } else if (keys.right.isDown) {

    if (keys.up.isDown) {
    } else if (keys.down.isDown) {

    // TODO: Clean this up
    if (keys.up.isDown || keys.down.isDown || keys.left.isDown || keys.right.isDown) {
      animationName = "player-walk";
    } else {
      animationName = 'player-idle';

    if(this.lastAnim !== animationName) {
      this.lastAnim = animationName;
      this.anims.play(animationName, true);

I instantiate it in the "create" method of the scene:

this.player = new Player(this, 300, 300, {key: 'player'});

The Sprite animates and everything, but it just stays in the same place.

My game setup:

const gameConfig = {
  width: 1024,
  height: 768,
  scene: GameScene,
  pixelArt: true,
  zoom: 2,
  physics: {
    default: 'arcade',
    arcade: {
        gravity: { y: 0 },
        debug: false

I've seen examples that create an update method then call it in the scene update loop, but shouldn't  preUpdate be the same? Any help appreciated!

Link to comment
Share on other sites

  • 2 years 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.

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.


  • Recently Browsing   0 members

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