O'Reilly logo

Cocos2d-x Cookbook by Akihiro Matsuura

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required