Phaser 3 Reference
May 5, 2020
Here’s a reference for creating components in your game using Phaser 3 using purely functional approach (no objects).
If you want to learn how to use Phaser 3, visit Phaser 3 tutorial.
Contents
This reference covers the following components:
- Images
- Sounds
- Text
- Bitmap Text (with customizeable fonts)
- Rectangles
- HTML Elements (scrollable)
This reference covers the following functionalities:
- Sprite sizes
- Anchor points
- Interactivity
Components
The following are code samples on how to use common Phaser 3 components.
Images
Add an image (e.g. jpeg, jpg, png) to your Phaser 3 scene and adjust image size.Add image
preload()
create()
Adjust image size
In fact, this code works for just about any sprite.
create()
Sounds
Add short sound effect (e.g. mp3, wav) to your Phaser 3 scene and play it during the game.Add sound
preload()
create()
Play sound
create()
Text
Add highlightable text to your Phaser 3 scene.Add Text
create()
Example
create()
Center text
create()
Bitmap Text
Add Bitmap text to your Phaser 3 scene which uses your own font.Prerequisite
1. Download a .ttf of your font
2. Go to Littera (Flash must be enabled), upload the .ttf file and obtain a font.fnt and font.bmp version of your font.
Add font
preload()
create()
Example
create()
Center Bitmap text
create()
Rectangles
Draw a rectangle into your Phaser scene, perhaps for a button or a frame.Add rectangle
create()
Example
create()
Take careful note of how to format colors in hexadecimal 0xff0000.
HTML Elements
Add an HTML page (e.g instructions panel, or menu) into your Phaser Game. This loads the element on top of the Phaser canvas.Add HTML element
Add this code to your main game config.
main.js
preload()
create()
Make HTML element scrollable
Perhaps your HTML element is a large page, and you want to make it fit into a small rectangle. In that case, you need format the element into a scrollable little element.
HTML_FILE.html
Functionalities
Here’s a reference for some of the common functionalities we want in our game components.
Anchor Points
Anchor point is the point where the location of your sprite is based on.Adjust Anchor Point
The following works for imageSprite, textSprite, rectangleSprite, and so on. This causes the anchor point of your sprite to shift.
create()
Sprite Sizes
Sprites, especially images, may not take up the correct size. The following code resizes the sprite to desired size.Change Sprite Size.
The following works for imageSprite, textSprite, rectangleSprite, and so on. This causes the sprite to have a different display size.
create()
Example
create()
Interactivity
Add buttons and interactions to your game, to trigger functions when clicked.Adding Interactivity
The following works for imageSprite, textSprite, rectangleSprite, and so on.
create()
Follow Object/Image Shape
If you want the click not to follow a rectangular bounding-box, and instead follow the shape of the object, then try altering the pixelPerfect property.
create()
Hand Cursor
If you want the cursor to turn into a hand when clicking the button, then change the useHandCursor this property.
create()