In third lesson we gonna do some nice stuff with famous Amiga ball 🙂
Update method is called once every Phaser Game loop. Update will be started after create method finishes its tasks.
It is fired every frame. Because of that you should optimize it very well and use it only for special purposes:
- checking collisions
- user input
It’s very important to understand what’s happening during update. You can check order of update events in Phaser.Game.updateLogic source code here.
Every element on the stage has its own preUpdate callback. Main preUpdate function located at fifth place in updateLogic is looping through all them.
Everything that will happen with Phaser object in next frame is prepared during this stage.
This function works similar to preUpdate.
Last place to change objects properties before frame render. Everything after this function will wiped out and rendered from scratch.
There’s one thing for which this function might be useful – grabbing last frame for further usage.
Check example to see what I’m writing about.
At this stage canvas is erased and everything is updated. Now you can add some post-processing effects or add debug data.
Render method of a plugin
It’s good practice to move your specific render code to a plugin. That’s because you can reuse it later.
Resize is a special method to handle stage size change (this only works in Phaser.ScaleManager.RESIZE scale mode). You should put here all code related to canvas size change relocate sprites etc.
This method is called when game begins to be paused for any reason (lose of browser window focus for example) or program change code using game.paused = true.
Check example to check how it can be used to display PAUSED text.
This might be used to hide PAUSED text mentioned before.
Is function that is running during pause state of a game. You can use it to add some effects to pause text.
This is a place to cleanup any external connections or large objects that can pollute memory. It’s called just before going to next state.
Please check example code on Github.