Creating a Sprite from a Sprite Sheet
A sprite sheet is a way to combine sprites into a single file. Using a sprite sheet helps achieve better performance by batching the draw calls. They can also save disk and video memory in cases where the sprites can be packed on a sheet more efficiently (generally requires special tools). You will read more about this in the Advanced Chapter, but it is one of many standard methods in the industry for increasing game performance.
When using a sprite sheet it is first loaded, in its entirety, into the
SpriteFrameCache is a caching class that retains the
objects added to it, for future quicker access. The
SpriteFrame is loaded
once and retained in the
Here is an example sprite sheet:
It doesn't look like much but let's take a closer look at what is happening:
As you can see the sprite sheet, at a minimum it reduces unneeded space and consolidates all sprites into a single file.
Let's tie this all together!
Loading a Sprite Sheet
Load your sprite sheet into the
SpriteFrameCache, probably in AppDelegate:
// load the Sprite Sheet auto spritecache = SpriteFrameCache::getInstance(); // the .plist file can be generated with any of the tools mentioned below spritecache->addSpriteFramesWithFile("sprites.plist");
Now that we have a sprite sheet loaded into
SpriteFrameCache we can create
Sprite objects by utilizing it.
Tools for creating Sprite Sheets
Creating a sprite sheet manually is a tedious process. Fortunately there are tools that can generate them automatically. These tools can provide even more ways to adjust your sprite sheet for maximum optimization!
Here are a few tools: