我如何在cocos2d中更改精灵的图像?

use*_*425 9 xcode cocos2d-iphone

我试过了

[[CCTextureCache sharedTextureCache] addImage: @"still.png"];
Run Code Online (Sandbox Code Playgroud)

但出于某种原因,我总是以失真的形象结束.这很可能是因为我的图像分辨率不同,但对于这个应用程序,它们不能拥有相同的分辨率.如何更改精灵的图像,而无需经历制作精灵表格或动画或其中任何一个的复杂过程.

Shi*_*han 16

urSprite = [CCSprite spriteWithFile:@"one.png"];
urSprite.position = ccp(240,160);
[self urSprite z:5 tag:1];

// Changing the image of the same sprite
[urSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"two.png"]];


Mar*_*rov 10

这是更改精灵图像的最直接的方法(如果你通过spritesheet加载它)这肯定有效(我在游戏中一直使用它).mySprite是精灵实例的名称:

[mySprite setDisplayFrame:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"sprite1.png"]];


tal*_*n11 6

你只需要调用sprite.texture函数.

在你的init方法中:

CCTexture2D *tex1 = [[CCTextureCache sharedTextureCache] addImage:@"still.png"];
CCTexture2D *tex2 = [[CCTextureCache sharedTextureCache] addImage:@"anotherImage.png"];
CCSprite *sprite = [CCSprite spriteWithTexture:tex1];
//position the sprite
[self addChild:sprite];
Run Code Online (Sandbox Code Playgroud)

然后将精灵的图像更改为tex2:

sprite.texture = tex2;
Run Code Online (Sandbox Code Playgroud)

非常简单!

希望这有帮助!