更改CCSprite的图像

use*_*578 33 cocos2d-iphone

我使用spriteWithFile创建了许多CCSprites.

如何在运行时更改精灵的图像?

我需要定期更改一些精灵图像.

小智 59

CCTexture *tex = [CCTexture textureWithFile:fileName];
self.texture = tex;
Run Code Online (Sandbox Code Playgroud)

  • @FaheemRajput ..如果精灵的大小不同,那么你需要在调用setTexture函数后执行此操作--- [spriteWant2Change setTextureRect:CGRectMake(0,0,tex.contentSize.width,tex.contentSize.height)]; - - (10认同)
  • 如果纹理与前一个不同,该怎么办? (5认同)
  • 任何人都知道如何在cocos2d 3.x中执行此操作?因为xcode不再找到CCTextureCache. (2认同)

Cle*_*v3r 14

如果您使用的是SpriteSheets,这将有效.

NSString* newSprite = [NSString stringWithString:@"SPRITE_NAME.png"];
CCSpriteFrameCache* cache = [CCSpriteFrameCache sharedSpriteFrameCache];
[sprite setDisplayFrame:[cache spriteFrameByName:newSprite]];
Run Code Online (Sandbox Code Playgroud)


Zee*_*han 9

[yourSprite setTexture:[[CCSprite spriteWithFile:@"yourImage.png"]texture]];
Run Code Online (Sandbox Code Playgroud)