Summary of the use of CCSprite in cocos2d

Source: Internet
Author: User

1. Wizard creation and initialization

1. Create an image file:

[Cpp]
CCSprite * sprite = [CCSprite spriteWithFile: @ "Icon.png"];
2. Create a frame cache:

[Cpp]
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @ "MineSweeping. plist"];
CCSprite * sprite = [CCSprite spriteWithSpriteFrameName: @ "Icon.png"];
3. Initialization and Custom Size

[Cpp]
CCSprite * sprite = [CCSprite spriteWithFile: @ "Icon.png" rect: CGRectMake (x, y, w, h)];
Note: The default positioning point is ccp (0.5, 0.5). The default position is ccp () and contentSize is the size of the sprite image.

2. Common attributes and methods of genie:

[Cpp]
[Self addChild: sprite]; // In the add-in layer, self is a CCLayer
Sprite. scale = 2; // zoom in 2 times. The parameter is proportional. 1 remains unchanged. 0.5 represents 50%, and 2 represents 200%.
Sprite. rotation = 90; // Rotate 90 degrees
Sprite. opacity = 255; // sets the transparency to completely opaque (range: 0 ~ 255)
Sprite. anchorPoint = ccp (0.5); // set the anchorPoint to the lower left corner. The default value is the center point of the ccp (0.5 ,).
Sprite. position = ccp (100,100); // set the coordinates in the lower left corner of the genie to x = 100, y = 100, and local GL coordinates.
[Sprite setFlipX: YES]; // reverse the X axis Image
[Sprite setFlipY: YES]; // the Y axis image is reversed.
[Sprite setVisible: NO]; // set to hide. The default value is visible.
[Sprite setColor: ccc3 (255, 0, 0)]; // set the color to red, primary color
[Sprite zOrder]; // The sprite cascade order is the Z axis (smaller and larger). Note that this is a read-only attribute and cannot be reset through sprite. zOrder = 2.
[Sprite setTextureRect: CGRectMake (10, 10, 30, 30)]; // coordinates of the start point (for the lift coordinate system), width and height
3. Add other genie

CCSprite inherits from CCNode, so you can perform addChild operations on it:

[Cpp
CCSprite * s1 = [CCSprite spriteWithFile: @ "Icon.png"];
CCSprite * s2 = [CCSprite spriteWithFile: @ "Icon.png"];
[S1 addChild: s2];
Iv. Resetting the zaxis of the genie

[Cpp]
[Self reorderChild: sprite z: 10]; // self is CCLayer
5. Change the sprite

1. directly use the new texture for replacement
[Cpp]
// Change the texture
CCTexture2D * texture = [[CCTextureCache sharedTextureCache] addImage: @ "Default.png"]; // create a texture
[Sprite setTexture: texture];
2. Use frame replacement

[Cpp]
// Load the frame Cache
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @ "MineSweeping. plist"];
// Retrieves default.png from the frame cache.
CCSpriteFrame * frame2 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @ "Default.png"];
[Sprite setDisplayFrame: frame2];


Author: wangqiuyun

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.