一個已經存在的CCSprite怎么替換新的圖片


// Obtain the shared instance of the cache
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache;

// load the frames
[cache addSpriteFramesWithFile:@"frames.plist"];

// It loads the frame named "frame1.png".
// IMPORTANT: It doesn't load the image "frame1.png". "frama1.png" is a just the name of the frame
CCSpriteFrame *frame = [cache spriteFrameByName:@"frame1.png"];
[sprite setDisplayFrame:frame];

 

 

一個已經存在的CCSprite怎么替換新的圖片

   如果一個CCSprite已經存在,這個時候如果要更新圖片,有一個方法是先remove,然后重新創建一個新的CCSprite,然后再add進去。
 
   其實有個新的方法,可以不需要重新創建CCSprite.
 
 
   這個方法名稱是:

-(void) setTexture:(CCTexture2D*)texture

 

 例子如下:

CCSprite *  headSprite=[CCSprite spriteWithFile:@"avatar_hall.png"];  
  
  
UIImage * image=[UIImage imageNamed:@"avatar_hall2.png"];  
  
  
CCTexture2D  * newTexture=[[CCTextureCache sharedTextureCache]  addCGImage:image.CGImage forKey:nil];  
  
[headSprite  setTexture:newTexture];  

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM