// 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 frameCCSpriteFrame *frame = [cache spriteFrameByName:@"frame1.png"];[sprite setDisplayFrame:frame];
如果一个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];