- Cocos2d-x Cookbook
- Akihiro Matsuura
- 513字
- 2021-07-09 20:58:48
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:
- Copy the image into the
Resource
folderMyGame/Resources/res
. - Open your project in Xcode.
- 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"); sprite->setPosition(Vec2(size.width/2, size.height/2)); this->addChild(sprite); return true; }
And then, after we build & run the project, we can see the following:

How it works...
You can get the screen size from the Director::getWinSize
method. The Director
class is a singleton class. You can get the instance using the getInstance
method. So you can get the screen size by Director::getInstance->getWinSize()
.
Sprites are made from images. You can create a sprite by specifying the image. In this case, you create the sprite by run_01.png
in the res
folder.
Next, you need to specify the coordinates of the sprite. In this case, you set the sprite in the center of the screen. The Size
class has the width and height property. You can specify the location of the sprite using the setPosition
method. The argument of the setPosition
method is Vec2
. Vec2
has two properties as floating point vector, x
axis coordinate and y
axis coordinate.
The last step is to add the sprite on the layer. A layer is like a transparent sheet on the screen. You will learn about layers in Chapter 4, Building Scenes and Layers.
All objects that are displayed on the screen are node. Sprite and Layer are types of node. If you haven't added it in the other nodes, the node does not appear on the screen. You can add a node in the other nodes by the addChild
method.
There's more...
You can set the sprite using the static coordinate. In the following case we see that the Sprite position is (100, 200)
.
sprite->setPosition(Vec2(100, 200));
Also, you can set the sprite in the center of the screen using C++ operator overloading.
sprite->setPosition(size/2);
If you want to remove the sprite from the layer, you can remove it by the following code:
sprite->removeFromParent();
See also
The Sprite
class has a lot of properties. You can manipulate them and change the sprite's appearance. You will also learn more about layer and the scene, which will be explained in Chapter 4, Building Scenes and Layers.
- LabVIEW Graphical Programming Cookbook
- TensorFlow Lite移動(dòng)端深度學(xué)習(xí)
- Learning Docker
- OpenCV for Secret Agents
- Getting Started with PowerShell
- 深度強(qiáng)化學(xué)習(xí)算法與實(shí)踐:基于PyTorch的實(shí)現(xiàn)
- HTML5+CSS3網(wǎng)頁(yè)設(shè)計(jì)
- Python編程實(shí)戰(zhàn)
- 響應(yīng)式Web設(shè)計(jì):HTML5和CSS3實(shí)戰(zhàn)(第2版)
- Maker基地嘉年華:玩轉(zhuǎn)樂(lè)動(dòng)魔盒學(xué)Scratch
- C指針原理揭秘:基于底層實(shí)現(xiàn)機(jī)制
- 基于MATLAB的控制系統(tǒng)仿真及應(yīng)用
- Unreal Engine Game Development Cookbook
- Vue.js項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)
- Performance Testing with JMeter 3(Third Edition)