15. Cocos2dx 3.0 game development look for the Sprite: Each genie is an angel of the wings of his life

Source: Internet
Author: User

 

Sprite
Sprite can be said to be the most important component of the game. It describes the genie in the game and is the most important and most flexible subclass of Node. Sprite is very important. It represents the smallest visible unit in the game, and Sprite is flexible. It loads a plane texture, has a wealth of expressiveness, and can be loaded in multiple ways. If Scene and Layer represent macro game Element Management, Sprite provides a wide range of flexible details for the micro-View world.
Texture is an image, which is displayed by the genie. Texture is the pattern drawn to the surface of the object in 3D games; The Cocos2d-x uses the 3D drawing library OpenGL; in this way, we can use the Graphics Accelerator to improve the drawing efficiency, you can also add 3D transform effects to the game to achieve more brilliant results. To draw a plane image in a 3D environment, the Cocos2d-x only needs to draw a rectangle perpendicular to the line of sight Plane in a 3D space, and use a texture map on the surface of the rectangle.
In the creation of the little angel game development application, the genie is created by a texture. If no settings are made for the genie, the genie is an image displayed on the screen. Generally, the genie is placed in the layer. Therefore, we prefer to create the genie in the initialization method of the layer, set attributes, and add them to the layer. The most common way to create a Sprite is to use an image file to create an Sprite by using the Sprite factory method;

auto bg= Sprite::create(bg.png);
This factory method contains a std: string parameter, indicating the file name of the texture used by the genie.
Sprite automatically loads the image into the game as a texture, and then uses the texture to initialize the genie. The genie not only displays a complete texture, but also only shows a part of the texture. Sprite * Sprite: create (const std: string & filename, const Rect & rect) can be used to achieve the goal;
The following code uses the create factory method of two parameters to create a genie: this genie uses bg.png as the texture, but only displays the 100x100 pixels in the upper left corner of the texture:
auto bg2= CCSprite::create(bg.png, Rect(0, 0, 100, 100));
The first parameter is the file name of the texture used, and the second parameter is a Rect type struct that indicates the rectangular part displayed in the texture. Rect (x, y, width, height) is used to easily create a rectangle. Note that the original point (0, 0) in the texture coordinate system is located in the upper left corner. The right of the origin is the positive direction of the x axis, and the downward direction of the origin is the positive direction of the y axis. The coordinate system will be described in detail later;
After creating genie for little angel clothes, we also need to arrange the genie in a proper place. Otherwise, the engine cannot decide how the genie will be presented. Parameters such as the location, direction, and scaling ratio of the genie are all attributes of the Genie. We need to set them properly before adding the genie to the layer. The following code first obtains the screen size, and then places the genie in the center of the screen according to the screen size:
Size size = Director::getInstance()->getWinSize();Point pos = Point(size.width / 2, size.height / 2);bg->setPosition(pos); 
We can see that in this code, we modified the Position attribute of the genie. The Position attribute is a Point type struct, indicating the Position of the genie in the layer. It is the coordinate of the genie relative to the layer. Set the coordinates of the genie bit to half the width of the screen, so that the genie can be placed in the center of the screen.
The genie has rich attributes that can be used to flexibly present the genie. But in fact, these attributes are not only owned by the genie, but also belong to the Node. Sprite inherits from CCNode and all its attributes.
After the angel arrives at the human community and sets the attribute of the genie, it is time to add the genie to the layer. In fact, both Sprite and Layer inherit from CCNode. addChild, which adds other game elements to a game element, is a Node-contained method. Therefore, we can add layers to a scenario just like adding layers, add the genie to the layer;
this->addChild(bg);

Other common members Sprite also has the following common members: 1. the initialization method CCSprite has many different initialization methods, allowing you to easily create genie. Using these methods, we can not only create an genie through an image file, but also directly create an genie using a texture or a Sprite frame. A. Use image files
static Sprite* create(const std::string& filename);static Sprite* create(const std::string& filename, const Rect& rect);
Filename indicates the image file name. You can directly input the path of the image file relative to the Resource folder. rect is an optional parameter used to specify the texture part of the sprite. It uses the texture coordinate system described earlier. B. Use Texture2D
static Sprite* createWithTexture(Texture2D *texture);static Sprite* createWithTexture(Texture2D *texture, const Rect& rect, bool rotated=false);
The texture parameter of the Texture2D type is a texture object. You can use the addImage method of the TextureCache class to load the image file as a texture and return the result. The rect parameter is the same as the rect parameter used to create the sprite. C. Use SpriteFrame to create
static Sprite* createWithSpriteFrame(SpriteFrame *spriteFrame);static Sprite* createWithSpriteFrameName(const std::string& spriteFrameName);
The SpriteFrame parameter of the spriteFrame type is a texture frame. SpriteFrame stores a reference of Texture2D and a Rect to show a part of the texture. When you use SpriteFrame to initialize the sprite, you can also make the sprite display part of the texture.
2. texture-related attributes Sprite provides the following texture-related attributes for obtaining or setting Sprite content. Texture2D * _ texture: gets or sets the texture used by the sprite. After you use this method to set the texture, the sprite will display a complete texture. Rect _ rect: gets or sets the texture display part. This Rect uses texture coordinates, that is, the upper left corner is the origin. SpriteBatchNode * _ batchNode: gets or sets the batch node to which the genie belongs.
3. texture-related methods Sprite provides the following texture-related methods. Virtual void setSpriteFrame (SpriteFrame * newFrame): sets the texture frame in the display. newFrame is a new texture frame, the texture or texture display part can be different from the old frame. Virtual SpriteFrame * getSpriteFrame () const: Get the texture frame being displayed. Virtual bool isFrameDisplayed (SpriteFrame * pFrame) const: returns a value indicating whether the pFrame is a texture frame being displayed.
4. color-related attributes Sprite provides the following color-related attributes: Color3B color: gets or sets the colors that are superimposed on the Sprite. Color3B consists of three colors (red, green, and blue. The default value is pure white, indicating that the color of the genie is not changed. If it is set to another value, the color of the genie is changed. GLubyte Opacity: gets or sets the Opacity of the genie. GLubyte is a built-in OpenGL type that represents an unsigned 8-digit integer ranging from 0 to 255. Bool isOpacityModifyRGB: gets or sets whether the texture data used by the genie has been premultiplied by the Alpha channel. If an image containing the Alpha channel is incorrectly displayed, you can modify this attribute.
Haomeng master friendly reminder: Daemon genie, daemon belongs to your angel ,,,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.