Cocos2d development and learning 5: spritebatchnode rendering and texture set texture atlases Memory Optimization

Source: Internet
Author: User

This chapter mainly introduces the concept of Sprite nodes and texture sets.

The simplest way to generate ccsprite is to use a texture map.

CCSprite *_firstSp = [CCSprite spriteWithFile:@"icon.png"]; CGSize wins = [[CCDirector sharedDirector] winSize];    _firstSp.position = ccp(_firstSp.contentSize.width/2, wins.height/2);    [self addChild:_firstSp z:1];

Animation Frame

The above is a basic method to generate the genie recently, but many times,

One of our Genie elements is not static, but only one frame of file,

In the form of a Sprite frame.

The sprite frame is an animation.

// Ccsprite * sp = [ccsprite spritewithfile: @ "ball0.png"]; // Step1: First, you must have an genie object. Of course, you can also use the above method, create a texture-initialized sprite object. The advantage of using the above method is that if you want to calculate the sprite location later, you can get the space size of the texture temporarily occupied. If you directly use node, the sprite occupies the space length and width should be all 0. Ccsprite * sp = [ccsprite node]; SP. position = cgpointmake (200,200); // Step 2: generate two frames based on the file name and put them into the array nsmutablearray * frames = [nsmutablearray arraywithcapacity: 2]; for (INT I = 0; I <2; I ++) {// obtain the texture map nsstring * file = [nsstring stringwithformat: @ "balladed.png", I]; cctexture2d * text2d = [[cctexturecache sharedtexturecache] addimage: file]; // generate the sprite frame ccspriteframe * frame = [ccspriteframe framewithtexture: text2d Rect: cgrectmake (0, 0, text2d. contentsize. width, text2d. contentsize. height)]; // put it into the array [frames addobject: frame];} // Step3: generate the animated GIF action ccanimation * ball = [ccanimation animationwithspriteframes: frames delay: 0.5]; ccanimate * animate = [ccanimate actionwithanimation: Ball]; ccrepeatforever * repeat = [ccrepeatforever actionwithaction: Animate]; // Step 4: run the animation frame. [Sp runaction: Repeat]; [self addchild: sp z: 1];

TIPS:

1. here, we can talk about how to get an animation frame by using a file name and then getting it from a resource file. Then, what information will tell you about the general resource naming rules, that is, using 0, 1, 2, 3, 4. I believe everyone understands it.

2. The early cocos2d version, the library used in this book, also has

/** Returns an animation given it's name .*/

-(Ccanimation *) animationbyname: (nsstring *) animationname;

/** Adds an animation to the sprite .*/

-(Void) addanimation: (ccanimation *) animation;

This method is used to store Frame Animation objects in the sprite node. However, this method does not appear to be available in the latest version.

Batch Processing (ccspritebatchnode)

The above is only for generating an genie node. Sometimes we need to create multiple identical genie in scenarios,

The most primitive method is to create one and add it to the scene.

The purpose of the sprite node is to put together some genie using the same texture and perform only one rendering to reduce the time required for rendering without creating a single Sprite.

CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithFile:@"ball.png"]; [self addChild:batch];for (int i = 0; i < 100; i++){CCSprite* sprite = [CCSprite spriteWithFile:@”ball.png”]; [batch addChild:bullet]; }

Ccspritebatchnode is like a layer. The sprite adds it and renders it at one time.

TIPS:

1. Texture maps must use the same file

2. Each sub-genie uses the same Z-order (depth ).

About texture map set (texture atlases)

To put it simply, for example, two images are required for two animations,

Then we load the image twice to read the two images from the resource file.

Why does texture set effectively reduce memory consumption,

This is because IOS automatically scales the image length and width to the Npower of 2 when loading the image's allocated memory. This is a good understanding, for example, the image of 16*35, the loaded memory is 16*64.

Therefore, if a texture set is used to reduce the memory size, it is not to say that you have merged several images into one texture set, and it will become smaller. It will be as big as a mess, except for a texture set, we only need to load it once, but if we load it multiple times, the memory consumption caused by "2 n" is obviously more dangerous than loading only once.

Texture set: Simply put, the small images that you originally scattered depend on a large image, the tool automatically generates a plist file to save the size of each cell Paster and the location information in the Paster.

Texture set creation tool. zwoptex is used in materials, but I found a "texturepacker" in the Forum ". Some people say they will charge fees, but it seems that downloading is free of charge. Do you charge fees for downloading apps in the appstore ~

Load texture Sets

CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];          [frameCache addSpriteFramesWithFile:@"xxxx.plist"];

In this way, a texture set is loaded and cached to the memory.

When a texture is required

CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];          CCSpriteFrame* frame = [frameCache spriteFrameByName:pngname];

TIPS:

1. this document provides a simple estimation of the memory size of an image. For example, for a 16*16 image, the memory size is 16*16*4 = 1kb. Haha, the information says 512*512*4 = 1 m ).

2. ccspriteframecache loads a spliced big image. Each small image is only a region in the big image. The region information is saved in the plist file.

You can load the image to this region based on the name of the thumbnail;

Cctexturecache is a normal image cache. By default, all images directly loaded are stored in this cache to improve call efficiency.

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.