IPhone game development Cocos2d image loading instance operations

Source: Internet
Author: User

IPhone gamesDevelopmentCocos2dLoadImageThis document describes how to operate instances.Cocos2dLoadImageThe method is actually very simple. I think it is quite practical. Let's look at the details.

First, we should remind you that,IhponeAll in developmentImageIt is best to use png. Although png images are larger than jpg or other images, png images have been optimized in the sdk and are recommended by Apple, the supported format is also relatively good. especially in the latest 4.2 sdk, if you have a jpg image and it is relatively large, it is impossible to come out on a real machine.

Start with the project.Cocos2dI know little about it. When I add a CCSprite, I directly use it for simplicity.

Java code

 
 
  1. CCSprite * sprite = [CCSprite spriteWithFile: @ "image name"];

CCSprite * sprite = [CCSprite spriteWithFile: @ "image name"]; if you use this method, the image will be released through the automatic release mechanism, when will the image be automatically released? Of course, when the program memory is about to run out

Java code

 
 
  1. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {     
  2.     [[CCTextureCache sharedTextureCache] removeUnusedTextures];     
  3. }    
  4.  
  5. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {  
  6.  [[CCTextureCache sharedTextureCache] removeUnusedTextures];  
  7. }  

Depending on the delegate method you have applied, there will be the above function, which is used to trigger the release of images. when the memory is about to reach the limit, a warning will be issued to inform the above aspect, and then this method will release the unused image from the cache,

When removeUnusedTextures enters this directory, it will actually release the retaincount = 1 resource. Through the log, it will find that the memory warning is classified,

At that time, level 1 was not a problem, and level 2 was dangerous, and more than Level 2 programs were finished. before calling the memory warning, you can use xcode to provide a memory tracking tool to find that images occupy the memory.

So don't rely on automatic release, it will harm the project, especially the games, there are a lot of pictures, if you load the materials in this way, it will be miserable in the future. I have a deep understanding.

I did this.

Java code

 
 
  1. CCTexture2D * backBGTexture = [[CCTextureCache sharedTextureCache] addImage:@"a_aboutBG.png"];     
  2. CCSprite * backgroundSprite = [[CCSprite alloc] initWithTexture:m_backBGTexture];     
  3. [self addChild:backgroundSprite];        
  4. [backgroundSprite release];    
  5.  
  6. CCTexture2D * backBGTexture = [[CCTextureCache sharedTextureCache] addImage:@"a_aboutBG.png"];  
  7. CCSprite * backgroundSprite = [[CCSprite alloc] initWithTexture:m_backBGTexture];  
  8. [self addChild:backgroundSprite];   
  9. [backgroundSprite release]; 

In fact, when the image is not used (usually in the dealloc method), remove the image directly.

Java code

 
 
  1. -(void)dealloc     
  2. {     
  3.     [[CCTextureCache sharedTextureCache] removeTexture:backBGTexture];     
  4.     [super dealloc];     
  5. }   

Summary:IPhone gamesDevelopmentCocos2dLoadImageThis article is helpful to you!

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.