Sign in to follow this  
lemmikens

Extending Phaser.GameObjects.Sprite Class?

Recommended Posts

Hi, I've checked a couple posts on this forum regarding extending classes in Phaser, but get confused on how it exactly works, below is the code I'm trying to implement to add stats to a sprite:

class spriteStats extends Phaser.GameObjects.Sprite {
  constructor (scene, x, y,myExtra)
  {
      super(scene, x, y);
      this.setTexture('../assets/testsprite.png');
      this.setPosition(x, y);
  }
  setStats(speed, jump){
    var stats = {
      "speed": speed,
      "jump": jump,
    }
  }
  getStats(){
    return stats
  }
}

But when I attempt to create the sprite:

var hero = this.add.spriteStats(100, 450, 'hero',0).setInteractive();
hero.setStats(5,5);

it gives me a: "Uncaught TypeError: this.add.spriteStats is not a function". I don't think I'm extending the class correctly. Please forgive me, as I'm still relatively new to JavaScript. 

Thanks!

Share this post


Link to post
Share on other sites

Samme, you have given so may correct answers to so many of these posts. Thanks for taking care of this one, too! Can I ask what exactly this does? Particularly the "existing part"

this.add.existing(...)

Much appreciated!

Share this post


Link to post
Share on other sites

Existing is a factory function, so your sprite gets added to the updateList, displayList, and associates some other things.

Rich did a great write up on Game Object Factories in a recent dev log, here; it outlines how to set up factory functions in your constructor.

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.