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

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.

主站蜘蛛池模板: 河源市| 正宁县| 东城区| 黄梅县| 策勒县| 阳高县| 嘉黎县| 通道| 平泉县| 玉环县| 防城港市| 黄陵县| 麟游县| 安西县| 泗阳县| 于都县| 乐亭县| 孟连| 柘荣县| 通许县| 叶城县| 新龙县| 张家界市| 永嘉县| 临西县| 玉林市| 富裕县| 阿克| 太谷县| 峨眉山市| 兴安盟| 临城县| 昆山市| 壤塘县| 吉隆县| 马鞍山市| 石嘴山市| 岑巩县| 河北省| 垦利县| 尼木县|