So I have wrote a post on how to make sprite sheets with canvas, which seems to work okay as a way to generate graphics to use in a phaser ce game project without loading an external image. However in this post I will be writing about how to go about doing so with phaser graphics display objects. Also for whatever the reason it might be nice to just generate textures in generate for whatever the reason using phaser graphics, so in this post I will be writing about some use examples of the generateTexture method of the Phaser Graphics class.
In the content of this post I am writing about a method in the graphics class of phaser ce that can be used to convert the current state of a graphics display object to a texture that can then be used to skin sprites. If you are new to phaser ce you might want to start with my getting started post on phaser ce, also if you are new to using graphics display objects you might want to start with my post on graphics in general as well.
In this post I am using phaser community edition 2.11.1 of phaser.
Whenever I create a graphics object the generateTextxure method can eb called to create an instance of PIXI.texture this can the be used anywhere where a texture can be used such as with a sprite.
In this example I cam creating a graphics object and making it so it is not visible. However I am creating a texture from that graphics object and then using it to skin a sprite that is visible.
So another use case example of Graphics.generateTexture would be to use it to create a sprite sheet with at least two or more frames. Doing so involves creating the state of the graphics that I want with the usual drawing methods, and then creating a texture like before for starters. However this time I need to pass the source property of the baseTexture property of the texture that is returned with Graphics.generateTexture. The image that is located at the source property is what I can pass as data for the game.cache.addSpriteSheet method.
Another thing about Graphics.generateTexture is that the resolution can be set with the first argument that by default has a value of 1. The documentation does not explain what the standard is with it, but it looks like it is a value that can be between 0 and 1.