HTML5 Bejeweled engine + Phaser array utilities = rotating bejeweled

Today I am going to show you how to merge the Match 3 Bejeweled HTML5 prototype I made a couple of months ago with the feature which allows to rotate a matrix explained in the post Understanding Phaser array utilities using ArrayUtils class to add a twist in Bejeweled games as I already showed you some years ago in the post Bejeweled AS3 engine with array rotation, back in old Flash AS3 days.

This is the result:

Make a match and see what happens. Before showing you the source code, here is a brief overview about the idea behind it:

* All orbs are inside a group, but unfortunately groups do not have anchor points because anchor point is a texture-related feature and groups do not have textures.

* No anchor point means we can’t rotate a group around its center, but only around its upper left corner, until…

* Groups have pivot points which are basically the same, with a different way to be defined: while anchor points are set with values from 0 to 1 where 0.5 is the center, pivot points must be defined in pixels, which means in some cases they are even more accurate.

* We create the group, we place the orbs then we set pivot point on its center and we move the group to the right and to the bottom by half its width and height.

* To make the stage rotate, we create a tween rotating the group by 90 degrees. This will only create the visual effect.

* Once the animation is completed, time to work behind the curtain by rotating the matrix, resetting group angle and reposition the orbs according to their new matrix position.

And that’s it:

Hope you can add a twist to your match 3 games, you can also download the source code of the entire project.

  • Hugeen


    Your source code is a bit messy. Especially the replenishField function which has 12 levels of indentation!

    I suggest you split your code into small (< 5-6lines, < 2-3 indentation levels) and well-named functions that are easy to read and understand.

    See you :-)

  • Richnou


    But I would just have a *small* question …

    How can I do to make the grid not at the top left (after changing the dimensions, to put an interface for example)?

    var offsetX = 150;
    var offsetY = 100;
    // …
    Phaser.Game (1024, 768);

    I tried to change the coordinates to various places, but without success …

    Would there be a simple solution?

    Thanks in advance..

  • Jas


    This is awesome. But would you know how to add “blockers” or “obstacles” like the candy crush game?