Phaser World Issue 27

Published on 22nd April 2016

lotus

After another round of bug fixing I'm very pleased that Phaser 2.4.7 is now released. Grab it from the Phaser site, GitHub or your favourite CDN. I wanted to use the space here this week to talk about a couple of its new features:

Camera Shake

Shake the camera by calling camera.shake. You can provide an intensity value, which controls how much the camera shakes. Smaller values mean less shake, and we're talking really small values here, like 0.01 for a 'slight tremor'. You can also specify the duration of the shake, given in milliseconds. The default is 500ms. There is a 'direction' argument, which allows you to shake the camera either just horizontally, vertically, or both. And finally 'shakeBounds'. This lets you control if the camera can shake the world beyond its bounds, revealing whatever your game background colour is behind it.

Internally the camera shake effect works differently depending if you're using Canvas or WebGL. In WebGL mode it simply uses the Pixi renderer offset property, changing it to whatever the shake values are. This is reset each frame. In Canvas it's a little different, instead the shake values are applied to the game objects transform matrix as they are rendered. It never updates the matrix, or adjusts the objects actual on-screen coordinates, it's purely applied during render, and reset again each frame.

Camera Flash

This is perfect for things like impact events. If the player gets hit by a bullet, then you could flash the screen red very quickly. It's handled by calling camera.flash. You can provide the colour to flash to (the default is white) and the duration. This effect works best for really short durations.

Camera Fade

A camera fade is likely more useful to help with transitions between scenes. For example facing the camera off to a nice black colour, then bring it back in again when the new scene has loaded. As you may expect by now you create the effect by calling camera.fade. As with flash you can specify the colour and duration of the effect. The difference is that once the effect has finished, your game will remain solidly filled with the colour of your fade. So if you fade to black, your game will remain utterly black (even if you change State), until you call camera.resetFX.

Internally the flash and fade effects work by adding a single Graphics object onto the display list, above the game world. This Graphics object contains a single rectangle shape. Each time the effect is called this rectangle is filled with the colour and opacity required, fading up (or down) to reach the desired colour, over the duration given. Once it reaches the given colour in a flash effect it has its alpha set to zero, which prevents it from being considered for rendering by the game render loop. The 'fx' graphics object sits above the World in the display list owned by the Stage. So if you have any code that adds additional objects to the stage display list, that you want to be impacted by a flash or fade effect, then you will want to be sure they are inserted correctly (after the world, before the fx object).

lotus

After another round of bug fixing I'm very pleased that Phaser 2.4.7 is now released. Grab it from the Phaser site, GitHub or your favourite CDN. I wanted to use the space here this week to talk about a couple of its new features:

Camera Shake

Shake the camera by calling camera.shake. You can provide an intensity value, which controls how much the camera shakes. Smaller values mean less shake, and we're talking really small values here, like 0.01 for a 'slight tremor'. You can also specify the duration of the shake, given in milliseconds. The default is 500ms. There is a 'direction' argument, which allows you to shake the camera either just horizontally, vertically, or both. And finally 'shakeBounds'. This lets you control if the camera can shake the world beyond its bounds, revealing whatever your game background colour is behind it.

Internally the camera shake effect works differently depending if you're using Canvas or WebGL. In WebGL mode it simply uses the Pixi renderer offset property, changing it to whatever the shake values are. This is reset each frame. In Canvas it's a little different, instead the shake values are applied to the game objects transform matrix as they are rendered. It never updates the matrix, or adjusts the objects actual on-screen coordinates, it's purely applied during render, and reset again each frame.

Camera Flash

This is perfect for things like impact events. If the player gets hit by a bullet, then you could flash the screen red very quickly. It's handled by calling camera.flash. You can provide the colour to flash to (the default is white) and the duration. This effect works best for really short durations.

Camera Fade

A camera fade is likely more useful to help with transitions between scenes. For example facing the camera off to a nice black colour, then bring it back in again when the new scene has loaded. As you may expect by now you create the effect by calling camera.fade. As with flash you can specify the colour and duration of the effect. The difference is that once the effect has finished, your game will remain solidly filled with the colour of your fade. So if you fade to black, your game will remain utterly black (even if you change State), until you call camera.resetFX.

Internally the flash and fade effects work by adding a single Graphics object onto the display list, above the game world. This Graphics object contains a single rectangle shape. Each time the effect is called this rectangle is filled with the colour and opacity required, fading up (or down) to reach the desired colour, over the duration given. Once it reaches the given colour in a flash effect it has its alpha set to zero, which prevents it from being considered for rendering by the game render loop. The 'fx' graphics object sits above the World in the display list owned by the Stage. So if you have any code that adds additional objects to the stage display list, that you want to be impacted by a flash or fade effect, then you will want to be sure they are inserted correctly (after the world, before the fx object).