Phaser for Beginners

Creating a preloader state

There’s another small optimization to do in order to keep your code organized, especially if you are working on a small game which does not require that much images and sounds. At the moment we preload assets in a way we can call “on demand”. When in title screen we needed the sound buttons, we preloaded their images, then when in game state we needed the sounds and the tiles, we preloaded them.

It would be better if we could preload all assets before the game begins, so all code and references to images and sounds can be found in a single place. That’s why are going to create a new state called PreloadAssets which will handle all preloading process, freeing other states from having a preload method.

Obviously this newly created state will be the first the game will execute, to ensure all assets have been preloaded before we launch the game itself with TitleScreen state.

The code of PreloadAssets is just a cut/paste of the code of the preload methods in PlayGame and TitleScreen states:

As you can see I only copied the content of the old preload methods – which you will have to remove now – and in create method, once all assets have been loaded, I launch TitleScreen state, starting the game.

Download Source

File: creating-preloader-state.zip


← Previous Tutorial

Organizing your folders

Leave a Reply

Your email address will not be published. Required fields are marked *