【发布时间】:2011-04-25 20:45:53
【问题描述】:
我试过了
[[CCTextureCache sharedTextureCache] addImage: @"still.png"];
但由于某种原因,我总是得到一个扭曲的图像。这很可能是因为我的图像分辨率不同,但对于这个应用程序,它们不能具有相同的分辨率。如何在不经历制作 spritesheet 或动画等复杂过程的情况下更改 sprite 的图像。
【问题讨论】:
标签: xcode cocos2d-iphone
我试过了
[[CCTextureCache sharedTextureCache] addImage: @"still.png"];
但由于某种原因,我总是得到一个扭曲的图像。这很可能是因为我的图像分辨率不同,但对于这个应用程序,它们不能具有相同的分辨率。如何在不经历制作 spritesheet 或动画等复杂过程的情况下更改 sprite 的图像。
【问题讨论】:
标签: xcode cocos2d-iphone
urSprite = [CCSprite spriteWithFile:@"one.png"]; urSprite.position = ccp(240,160); [自我 urSprite z:5 标签:1]; // 改变同一个精灵的图像 [urSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"two.png"]];
【讨论】:
这是更改精灵图像的最直接方法(如果您通过精灵表加载它),这绝对有效(我在游戏中一直使用它)。 mySprite 是精灵实例的名称:
[mySprite setDisplayFrame:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"sprite1.png"] ];
【讨论】:
您只需调用 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];
然后将精灵的图片改为tex2:
sprite.texture = tex2;
很简单!
希望这有帮助!
【讨论】:
在 cocos2d v3 中,我能够通过...完成此操作
[mySprite setSpriteFrame:[CCSpriteFrame frameWithImageNamed:@"two.png"]]
...但我不知道这是否会产生长期伤害我的副作用。 :)
【讨论】:
这个简单的一行可以完成你的任务。
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"slot.png"]];
【讨论】:
我使用 cocos2d 3.0,这段代码对我有用:
[_mySprite setTexture:[CCTexture textureWithFile:@"myFile.png"]];
【讨论】:
在 Cocos2d-x v3 中,可以使用my_sprite->setTexture(sprite_path);
【讨论】: