Here's a screen shot of the input panel running:
There are the standard letters A to Z to select from, along with the period character and a hyphen. The RUB icon will erase one letter, and END commits your name to legend.
The block, around the letter A in the screen shot above, is our guide to show which letter we're currently over. If we move a pointer over it, it'll change to track whatever letter we're currently hovering over. Equally, we can press the cursor keys to move it too.
Let's construct the panel.
The letters in the panel come from a Bitmap Font I created using the BMFont tool and a True Type font:
You're welcome to use this font, of course, but you could replace it with another. If you do, you'll have to adjust the coordinates in the code, but I've put comments in to show where.
The other sprites (the block, RUB and END) are just simple PNGs created at the correct size for the font. For this example, I'm going to create a Highscore Scene, that simply loads the assets and listens for events from the Input Panel. Here is the complete class:
class Highscore extends Phaser.Scene {
constructor ()
{
super({ key: 'Highscore', active: true });
this.playerText;
}
preload ()
{
this.load.image('block', 'block.png');
this.load.image('rub', 'rub.png');
this.load.image('end', 'end.png');
this.load.bitmapFont('arcade', 'arcade.png', 'arcade.xml');
}
create ()
{
this.add.bitmapText(100, 260, 'arcade', 'RANK SCORE NAME').setTint(0xff00ff);
this.add.bitmapText(100, 310, 'arcade', '1ST 50000').setTint(0xff0000);
this.playerText = this.add.bitmapText(580, 310, 'arcade', '').setTint(0xff0000);
// Do this, otherwise this Scene will steal all keyboard input
this.input.keyboard.enabled = false;
this.scene.launch('InputPanel');
let panel = this.scene.get('InputPanel');
// Listen to events from the Input Panel scene
panel.events.on('updateName', this.updateName, this);
panel.events.on('submitName', this.submitName, this);
}
submitName ()
{
this.scene.stop('InputPanel');
this.add.bitmapText(100, 360, 'arcade', '2ND 40000 ANT').setTint(0xff8200);
this.add.bitmapText(100, 410, 'arcade', '3RD 30000 .-.').setTint(0xffff00);
this.add.bitmapText(100, 460, 'arcade', '4TH 20000 BOB').setTint(0x00ff00);
this.add.bitmapText(100, 510, 'arcade', '5TH 10000 ZIK').setTint(0x00bfff);
}
updateName (name)
{
this.playerText.setText(name);
}
}
In preload
we grab the assets required. In create
we are adding some Bitmap Text objects to display the table legends (Rank, Score, Name), then we create a new Bitmap Text called playerText
. This is the text that will hold the name the user enters.
Finally, we launch the InputPanel
Scene, and listen for two events from it. The first, updateName
is triggered whenever the user enters a character (or erases one). When this happens we just update our playerText
Bitmap Text to show the new name.
The event submitName
is triggered when they've finished. In the example code above we stop the Input Panel Scene and just display a few more scores below the users. In your actual game, you could do whatever you like here.
In the next step we'll create the Input Panel.