Use ccsprite in cocos2d

Source: Internet
Author: User

1. Wizard creation and initialization

1. Create an image file:

Ccsprite * sprite = [ccsprite spritewithfile: @ "imagefilename.png"];

Default anchor CCP (0.5, 0.5 ),
Default position: CCP (0, 0 ),
The ccsprite size (contentsize) is the image size.

2. Create a frame cache:

[[Ccspriteframecache sharedspriteframecache] addspriteframeswithfile: @ "minesweeping. plist"];

Ccsprite * sprite = [ccsprite spritewithspriteframename: @ "imagefilename.png"];

3. Initialization and Custom Size

Ccsprite * sprite = [ccsprite spritewithfile: @ "imagefilename.png" rect: cgrectmake (X, Y, W, h)];

Only show part of the image, W, h

The texture size of the IOS device must comply with the "2 n" rules, so the texture width and height must be 2, 4, 8, 16, 32, 64, 128,256,512,102 4. The third-generation device can reach 2048 pixels.

2. Common attributes and methods of genie:

Add sub-nodes. ccsprite inherits from ccnode and can perform addchild operations.

[Self addchild: SPRITE];

Set the ccsprite position, local GL coordinate system,

Sprite. Position indicates the position of the sprite's central point on the GL series.

[S setposition: CCP (x, y)];

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.

Scale (the parameter is proportional, 1 remains unchanged, 0.5 stands for 50%, and 2 stands for 200%)

Sprite. Scale = 2; // enlarge 2 times


Rotate

Sprite. Rotation = 90; // Rotate 90 degrees

Set transparency (range: 0 ~ 255)

Sprite. Opacity = 255; // sets the transparency to completely opaque (range: 0 ~ 255)

Set the ccsprite anchor in the lower left corner:

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

Enable ccsprite Image

[Sprite setflipx: Yes]; // reverse the X axis Image

[Sprite setflipy: Yes]; // the Y axis image is reversed.

Visible or not

[Sprite setvisible: No]; // set to hide. The default value is visible.

Set the ccsprite (image) color

[Sprite setcolor: ccc3 (255, 0, 0)]; // set the color to red, primary color

The cascade order of ccsprite (smaller in order, larger in above)

[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.

Set texture size

[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:

Ccsprite * S1 = [ccsprite spritewithfile: @ "icon.png"];

Ccsprite * S2 = [ccsprite spritewithfile: @ "icon.png"];

[S1 addchild: S2];

Iv. Resetting the zaxis of the genie

[Self reorderchild: sprite z: 10]; // self is cclayer or ccnode

5. Change the sprite

1. directly use the new texture for replacement

// Change the texture

Cctexture2d * texture = [[cctexturecache sharedtexturecache] addimage: @ "default.png"]; // create a texture

[Sprite settexture: texture];

2. Use frame replacement

// 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];

6. Remove sprite:

-(Void) spritemovefinished :( ID) sender {

Ccsprite * sprite = (ccsprite *) sender;

[Self removechild: SPRITE cleanup: Yes];

}

7. Sprite batching ):

Create multiple ccsprite nodes and add them to the same ccspritebatchnode to increase rendering speed

Ccspritebatchnode * batch = [ccspritebatchnode batchnodewithfile: @ "bullet.png"];

[Self addchild: Batch];

For (INT I = 0; I <100; I ++)

{

Ccsprite * sprite = [ccsprite spritewithfile: @ "bullet.png"];

[Batch addchild: bullet];

}

Ccsprite * sprite = [ccsprite spritewithfile: @ "bullet.png"];

[Batch addchild: bullet];

When should I use ccspritebatchnode?

When you need to display two or more identical ccsprite nodes, you can use ccspritebatchnode. The more ccsprite nodes that are combined together, the more improved the result obtained by using ccspritebatchnode.

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.