HTML5 String Avoider game made with Phaser

After the Stencyl version, it’s time to create an HTML5 String Avoider game, using the latest release of Phaser, 2.0.5 at the moment.

The source code is actually a direct port of my AS3 String Avoider version, so I will only comment the lines with actually show something different than the original example.

First, let’s see what we are going to do:

As usual, move the string with the mouse and avoid the maze.

Here is the source code:

And now let’s see the interesting lines of this script:

Lines 26-29: unfortunately JavaScript does not support Point variable type, so I am creating new nodes as objects.

Line 35: creation of the bitmap data which will be used to draw the maze

Line 36: finally we draw the maze. Actually, we just paste it from the image loaded at line 24

Line 37: updating the bitmap data. This is very important! Since the bitmap content changed by drawing the maze, it needs to be updated in order to have pixel collision working properly.

Line 60: the core of the game: checking collisions by looking at the pixel under the string to see if it’s transparent.

There is a lot to say about it: first, you must use integer coordinates (that’s why I rounded string values).

Second, it does not work on my iPhone, both using Safari and Chrome. This is really a pity because on my iPhone 5 string movement looks really smooth and could be used for a game, but I wasn’t able to find a way to have pixel color check to work.

Hope someone will found a fix to make it work on iOS browsers.

Meanwhile, download the source code with all libraries included.