官术网_书友最值得收藏!

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:

Getting ready

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 to do it...

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

Tip

Please note that you can get an instance of a singleton class in Cocos2d-x using the getInstance method.

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.

主站蜘蛛池模板: 金坛市| 洪雅县| 牟定县| 大安市| 安丘市| 红安县| 龙井市| 白城市| 双江| 林口县| 老河口市| 慈利县| 无为县| 两当县| 冕宁县| 定远县| 潞西市| 自治县| 临沂市| 嵊州市| 饶平县| 得荣县| 华阴市| 海兴县| 涞水县| 蓝山县| 平阴县| 石棉县| 七台河市| 雅安市| 云安县| 新宁县| 龙口市| 肥西县| 祁东县| 文山县| 景谷| 辽中县| 当涂县| 昔阳县| 崇礼县|