Subclassing a Phaser.Sprite

  • 1 Min. Read.

Inheritance in Javascript can be a bit confusing if you come from a class-based language (C++ for example).

When it comes to inheritance, JavaScript only has one construct: objects. Each object has an internal link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. null, by definition, has no prototype, and acts as the final link in this prototype chain. Source:

When I was developing a game in the PhaserJS framework, I found myself in a position where I wanted to create a subclass of  Phaser.Sprite  and came up with the following template.

Now when we want to create an instance of MySprite and add it to the stage, we just do the following.

And there we have it! A template for subclassing Phaser.Sprite!