“Tipsy Tower” HTML5 prototype made with Phaser and Box2D – Step 2: camera zoom

Welcome to the second step in the Tipsy Tower series made with Phaser.

In first step we created the ground, the moving crate, the falling crates and the physics environment.

Now it’s time to make the game zoom out when you manage to stack crates or soon you will have your moving crate below the highest crate on the stack. Moreover, you won’t be able to see the entire tower you built with crates because it won’t fit on the screen.

Remember the game uses Phaser’s awesome Box2D premium plugin which will allow you to create physics driven games in a few lines, using the most famous 2D rigid bodies physics engine in the world.

In the original game, the camera zooms – and the player can drop another crate – when all crates are sleeping.

To save resources, Box2D puts bodies to sleep when nothing is happening to them: no collisions, no force applied, nothing. Sleeping bodies are excluded from the simulation – increasing efficency – until something happes to wake up them again. This could be a collision, or a force applied to the body.

In my opinion this makes the gameplay a bit slow, because – expecially when you have a lot of crates on the stage – it could take some seconds for all bodies to sleep, which means you can’t play for some seconds.

My version of the prototype zooms once the latest crate you dropped collides with something or falls off the stage. This way I can reduce pauses in gameplay and make the game more dynamic.

Have a look at the game:

Click to drop the crate. If you have a mobile phone, play the prototype directly at this link.

Unlike Unity, Phaser does not have a camera able to zoom, so we need to create a group with all game assets and play with group scale.

Also, every crate has a hit custom property initially set to false, which becomes true once it collides with something. The highest crate with hit property set to true will determine the zoom, which as said it’s just a tween which scales the game group.

Have a look at the source code, still needing some optimization because it has too much hardcoded values:

Although not that good to read, it does its job and in the end we have our zooming camera. Next time, I will show you how do to the same thing with Unity, it would be nice to complete the prototype both with Phaser and Unity, then compare the way they were made. Download the source code and give me feedback.