Using a texture atlas
A texture atlas is a large image containing a collection of each sprite. We often use a texture atlas rather than individual images. In this recipe, you will learn how to use a texture atlas.
Getting ready
You have to add the texture atlas files into your project and clean your project.
running.plist
running.png
How to do it...
Let's try to read the texture altas file and make a sprite from it.
auto cache = SpriteFrameCache::getInstance(); cache->addSpriteFramesWithFile("res/running.plist"); auto sprite = Sprite::createWithSpriteFrameName("run_01.png"); sprite->setPosition(size/2); this->addChild(sprite);
How it works...
Firstly, we loaded the texture atlas file, when the SpritFrameCache
class cached all the images that are included in it. Secondly, you generated a sprite. Do not use the Sprite::create
method to generate it, use the Sprite::createWithSpriteFrameName
method instead. Then, you can handle the sprite as a normal sprite.
A texture atlas is a large image...