Sprite classes in Cocos2d-x Lua

Source: Internet
Author: User

Sprite classes in Cocos2d-x Lua
The Sprite class is shown in its class diagram.

Sprite graph Sprite class directly inherits the Node class and has the basic features of Node. In addition, we can also see that the Sprite class subclasses include PhysicsSprite and Skin. PhysicsSprite is a physical engine genie class, and Skin is a Skin sprite class used for skeleton animation.
Create a Sprite object
There are multiple ways to create a genie object. Common functions are as follows:
Cc. Sprite: create (). Create a Sprite object. The texture [texture (texture) represents one or several two-dimensional images of the object's surface details, also known as texture maps, when you map a texture to the object surface in a specific way, the genie looks more real. ] And other attributes must be set after creation.
Cc. Sprite: create (filename ). Specify the image creation wizard.
Cc. Sprite: create (filename, rect ). Specify the image and the cropped rectangular area to create the sprite.
Cc. Sprite: createWithTexture (texture ). Specify the texture creation wizard.
Cc. Sprite: createWithTexture (texture, rect, rotated = false ). Specify the texture and the cropped rectangular area to create the sprite. The third parameter indicates whether to rotate the texture. By default, the texture is not rotated.
Cc. Sprite: createWithSpriteFrame (pSpriteFrame ). Create another sprite object through one sprite frame object.
Cc. Sprite: createWithSpriteFrameName (spriteFrameName ). Create a Sprite object by specifying the sprite frame name in the frame cache.
The above create function is described in the previous chapter, and the create FUNCTION is relatively simple, so we will not introduce it again.
Example: Use a texture object to create a Sprite object
The createWithTexture function is used to create a Sprite object using a texture Texture2D object. This section describes how to use a texture object to create a Sprite object, as shown in this example. The grass on the ground is placed in the background, the two trees in the scene are taken from the "Tree" texture image shown in the figure. They are the texture coordinates of the tree. Note that the coordinate origin is in the upper left corner.

Create a Sprite object instance
Background Image


"Tree" Texture Image
"Tree" texture coordinates

The createLayer Function Code implemented by HelloWorldScene. cpp is as follows:

function GameScene:createLayer()    local layer = cc.Layer:create()    local bg = cc.Sprite:create(background.png)①    bg:setPosition(cc.p(size.width/2, size.height/2)) ②    layer:addChild(bg)    local tree1 = cc.Sprite:create(tree1.png,cc.rect(604, 38, 302, 295)) ③    tree1:setPosition(cc.p(200,230))    layer:addChild(tree1,0)    local cache = cc.Director:getInstance():getTextureCache():addImage(tree1.png)④    local tree2 = cc.Sprite:create()⑤    tree2:setTexture(cache) ⑥    tree2:setTextureRect(cc.rect(73, 72,182,270)) ⑦    tree2:setPosition(cc.p(500,200))    layer:addChild(tree2,0)    return layerend
In line ① Of the above Code, cc. Sprite: create(background.png.pdf use background.pngimage to create a spirit. The background.png image is shown in the preceding figure. The line ② code is used to set the background position.
Line 3: cc. Sprite: create(tree1.png, cc. rect (604, 38,302,295 bytes). The Sprite is created through the tree1.png image and the rectangular cropping area. The rectangular cropping area is (604, 38,302,295 ),.
The rect class can create a rectangular cropping area. The rect constructor is as follows:
Cc. rect (x, y, width, height)
X and y are UI coordinates, the coordinate origin is in the upper left corner, width is the width of the cropped rectangle, and height is the height of the cropped rectangle.
The Code in line ④ creates a texture Texture2D object through the texture cache TextureCache. The TextureCache instance can be obtained through the getTextureCache () function of ctor, And the TextureCache watermark is the texture image name.

The Code in line ⑤ creates an empty Sprite object, so you need to set its attributes through many functions following it. The Code in line 6 tree2: setTexture (cache) is used to set the texture. Line 7 Code tree2: setTextureRect (cc. rect (73, 72,182,270) is the cropping area for setting the texture.

 

 

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.