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: developer.mozilla.org

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!

A concept by Wonderlus

Warning: Parameter 1 to W3_Plugin_TotalCache::ob_callback() expected to be a reference, value given in /mnt/home_bingo/codecaptain/codecaptain.io/public/wp-includes/functions.php on line 3583