Book Image

Cocos2d-x Cookbook

By : Akihiro Matsuura
Book Image

Cocos2d-x Cookbook

By: Akihiro Matsuura

Overview of this book

Table of Contents (18 chapters)
Cocos2d-x Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating sprites


Sprites are the most important things in games. They are images that are displayed on the screen. In this recipe, you will learn how to create a sprite and display it.

Getting ready

You can add the image that you made in the previous chapter into your project, by performing the following steps:

  1. Copy the image into the Resource folder MyGame/Resources/res.

  2. Open your project in Xcode.

  3. Go to Product | Clean from the Xcode menu.

You have to clean and build when you add new images into the resource folder. If you did not clean after adding new images, then Xcode will not recognize them. Finally, after you add the run_01.png to your project, your project will be seen looking like the following screenshot:

How to do it...

We begin with modifying the HelloWorld::init method in the following code:

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    Size size = Director::getInstance()->getWinSize();
    auto sprite = Sprite::create("res/run_01.png"...