Extend a sprite in Phaser 3

How to extend a sprite in Phaser 3

Sometimes just adding a sprite into a game is enough. Other times it is very useful to extend a sprite.

Why? Being able to do this will let us get super organized. That allows us to build games faster and better. We can make complex classes that extend Phaser game objects such as sprite, text or images. Most importantly we can reuse the code!

In my experience using classes allows us to do more by writing less code. If well written, the class can be transferred from one game to another without changing the code in the class.

Extending existing classes can be very powerful. I’m going to show you the basics in this post of how to extend a sprite in Phaser 3.

For this example, I am using a basic template. I have a sprite which is a bomb in the middle of the screen. There are just 2 lines of code added. One to preload the image, and one to place it on the screen

And here is the result:

Bomb Sprite

Make the class and extend a sprite

Now let’s make a bomb class by making a new file called bomb.js

The class name bomb and we extend it with the sprite class by using Phaser.GameObjects.Sprite

Then we need a constructor to fire when the bomb is created. Inside that constructor then we have to call the super. A super is the constructor inside the class we are extending, in this case, the sprite.

The super requires a scene as it’s the first parameter. We also need to give it an x and a y position and an image key that we’ve already preloaded. To get those values we are going to pass those into our bomb class through the constructor. We can do this using a single object called ‘config’.

For the key, since we’re only using this for a bomb we can just hard code that in. If you want you can pass in the key with the config object.

and we need to link this bomb class into the index.html file

Adding to the scene

The last thing to do is to add the sprite to the scene as an existing object. Otherwise, it will not show up

Now back in our scene main, we can comment out the code in create and instead say

Adding new functions

So that does exactly the same thing as just adding the sprite. The difference is that now we can organize our code better. We can just add things that just deal with the bomb inside of here. For example, we can say make the bomb sprite interactive by adding some code to the constructor. Let’s also add a click listener and function to decrease the alpha of the bomb every time it is clicked.

Now we can click until the bomb is gone.

faded bomb

While this is just a simple example, just image making a more complex class. For example one of a spaceship. You can add all the functions of turning, moving and firing. You then just need to add that to any game that class is needed. You save yourself tons of work this way!

Making a lot of instances

Let’s make more bombs here by using a for-loop. In fact, let’s make 100 of them, and we will pick some random positions for each bomb.

Here is the result. Only the bomb we click on actually takes any action. This is because each one is a separate instance of that bomb class!

100 bombs

This is very helpful sometimes when you’re building a more complex project and you can keep everything inside that. It also makes it
easier to reuse things. Any functions I put in the class can possibly be used for a different game. All I need to do is just change the key.

That is How you extend a sprite in Phaser 3!

Extending a sprite checklist

  • Make a new file
  • Make a class
  • Extend Phaser.GameObjects.Sprite
  • In the constructor called the super with the scene, an x and a y position, and a key
  • Add the class to the scene with config.scene.add.existing(this)

Leave a Reply