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