HTML5 Games Workshop


In this step we are going to add a scoreboard that displays how many coins the main character has collected:

Coin scoreboard

In order to do that, we need to be able to write text in the screen. In games, this can be done in different ways:

For the scoreboard we will use a bitmap font, which are called in Phaser retro fonts. The font will consist only of digits, a blank space and an x character. Here's the spritesheet:

Bitmap font spritesheet

It's important to know that in order to render a text with a bitmap font, we need both an instance of Phaser.RetroFont and an instance of Phaser.Image. Why? The retro font holds the image data in memory (i.e. the pixel values of a rendered text), but then we need a Phaser entity that can make use of that image data, such as Phaser.Image (or even Phaser.Sprite)!


Keep track of how many coins have been collected

  1. We only need a property in PlayState to do this. We are initialising it to zero in init, and then increasing this counter when a coin is picked up:

     PlayState.init = function () {
         // ...
         this.coinPickupCount = 0;
     PlayState._onHeroVsCoin = function (hero, coin) {
         // ...

Draw a coin icon on top of everything

  1. Load the image asset in preload:

     PlayState.preload = function () {
         // ...'icon:coin', 'images/coin_icon.png');
         // ...
  2. We will separate the creation of UI elements into a separate method. Inside it, we will create a new group to store all the UI icons, text, etc.

     PlayState._createHud = function () {
         let coinIcon =, 0, 'icon:coin');
         this.hud =;
         this.hud.position.set(10, 10);

    Note how all entities inside this.hud will get rendered relatively to it. This means that, since the hud is in position (10, 10), if we draw an image at –for instance– (5, 5), it will get rendered at position (15, 15) of the screen.

  3. Since the HUD must be rendered on top of everything else, it should be created after spawning all the elements in the level:

     PlayState.create = function () {
         // ...
  4. Check that the coin icon is rendered at the top left of the screen:

    HUD with coin icon

Write the text

  1. Finally we get to the most interesting part! As usual, we need to load the asset that will make up the font. Note that, even though conceptually it is a spritesheet, in Phaser it needs to be loaded it with load.image:

     PlayState.preload = function () {
         // ...'font:numbers', 'images/numbers.png');
         // ...
  2. Now we need to instantiate Phaser.RetroFont, that will be able to compute how a text looks like with the bitmap font spritesheet.

     PlayState._createHud = function () {
         const NUMBERS_STR = '0123456789X ';
         this.coinFont ='font:numbers', 20, 26,
             NUMBERS_STR, 6);
         // ...

    Since Phaser has no idea of the contents of the spritesheet, we need to tell it when creating the retro font: the width and height of each character and which characters are being included (the orden is important!)

  3. With the retro font created, we need to make use of it from a game entity. We will use a Phaser.Image for this:

     PlayState._createHud = function () {
         // let coinIcon = ...
         let coinScoreImg = + coinIcon.width,
             coinIcon.height / 2, this.coinFont);
         coinScoreImg.anchor.set(0, 0.5);
         // ...
  4. Last, we just need to tell the retro font which text string to render.

     PlayState.update = function () {
         // ...
         this.coinFont.text = `x${this.coinPickupCount}`;

Try it in the browser and see how the text changes with every coin collected!

Level with coin score board


Are you stuck? Take a look at the source code for this step.