In many game projects there is a need to create a grid, either as part of the games mechanics, as a way to dimension space, or as a way to quickly have some way to show that a sprite is moving my having at least some kind of background other than a solid color. In Phaser ce there is a way to quickly make a grid texture with create.grid, but this might not be the best option for some cases. In any case in this post I will revive a quick simple demo of how to use create.grid, and some pother alternatives as well.
In this post I am using phaser community edition 2.11.0, and yes phaser is the kind or project where the version number matters big time, most of all with the major release number. Always check the version number of the revision of phaser that you are using first if you run into any problems.
Although this method works, it is a little buggy at least in 2.11.0. When the call back fires it will work if I use the reference to the texture that is based to the call back, but any key that I set will not work as a way to use the texture. Never the less it is a phaser built in way to quickly generate a grid texture, it’s just that I very quickly become more interested in using a better options for generating a grid, and not just because of this one little issue with the key value that is given as the first argument that I just leave as an empty string. There is also no option to set the line width, or any fill texture as well.
Still as a quick brainless solution for making a grid in a flash, it does get the job done. Later on in development depending on the project the gird might be removed, or replaced with something more interesting anyway. In which case this can serve it’s intended purpose okay.
If for some reason you do not like create.grid, then you might consider going with a canvas powered solution. I like doping this often for many of my simple examples on this site because I am comfortable with the 2d canvas drawing context, but there are many other ways of drawing a grid on the fly with things the the Graphics display objects, and create.texture as well.
So create.grid is an okay little feature for quickly drawing a grid in phaser ce, but it might not be the best solution for doing so in some cases. Also it is not at all a replacement for tile maps, or having a group or sprites aligned into a grid like pattern.