如何切换CCSprite的图像 [英] How to switch the image of a CCSprite

查看:128
本文介绍了如何切换CCSprite的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CCSprite,使用 [CCSprite spriteWithSpriteFrameName:@plist_file_key_here.png] 初始化。我已经添加了所有的精灵从我的plist文件到CCSpriteFrameCache。我已经尝试设置纹理像这样:

I have a CCSprite that is initialized using [CCSprite spriteWithSpriteFrameName:@"plist_file_key_here.png"]. I have already added all the sprites from my plist file to CCSpriteFrameCache. I have tried setting the texture like this:

CCSpriteFrame * frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:name];
NSAssert(frame.texture!=nil, @"frame.texture can't equal nil"); //this works fine
[sprite setTexture:frame.texture]; //doesn't cause a white square to appear, just doesn't switch the image.

正如我在我的意见中说的,这不工作。我认为它与使用 [CCSprite spriteWithFile:] [CCSprite spriteWithSpriteFrameName:] 之间的区别有关,这依赖于从纹理地图集加载到CCSpriteFrameCache的精灵帧。当使用从纹理图集加载的sprite时,每个sprite的纹理等于sprite sheet的纹理。有什么办法围绕这个或者我必须删除和重新创建精灵?如果这是我唯一的选择,有没有办法从其父级删除一个ccnode但保留其子级?

As I said in my comments, this doesn't work. I think it has something to do with the difference between using [CCSprite spriteWithFile:] and [CCSprite spriteWithSpriteFrameName:], which relies on sprite frames loaded into the CCSpriteFrameCache from a texture atlas. When using sprites loaded from a texture atlas, the texture of each sprite is equal to the texture of the sprite sheet. Is there any way around this or do I have to remove and recreate the sprite? If that is my only option, is there a way of removing a ccnode from its parent but preserving its children?

推荐答案

API参考资料救援!

当你有一个纹理与精灵帧,你不想改变纹理,但sprite帧精灵使用。你可以这样做:

When you have a texture with sprite frame, you don't want to change the texture but the sprite frame the sprite uses. That you can do as follows:

CCSpriteFrameCache* cache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCSpriteFrame* frame = [cache spriteFrameByName:name];
sprite.displayFrame = frame;

它需要是:

sprite.spriteFrame = frame;

这篇关于如何切换CCSprite的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆