如何切换CCSprite的图像
我有一个使用 [CCSprite spriteWithSpriteFrameName:@"plist_file_key_here.png"]
初始化的 CCSprite。我已经将 plist 文件中的所有精灵添加到了 CCSpriteFrameCache。我尝试过像这样设置纹理:
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 中的精灵帧阿特拉斯。当使用从纹理图集加载的精灵时,每个精灵的纹理等于精灵表的纹理。有什么办法可以解决这个问题,还是我必须删除并重新创建精灵?如果这是我唯一的选择,是否有办法从其父节点中删除 ccnode 但保留其子节点?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
救援 API 参考!
当你有一个带有精灵框架的纹理时,您不想更改纹理,而是更改精灵使用的精灵框架。你可以这样做:
在 cocos2d v3 中,它需要是:
The API Reference to rescue!
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:
in cocos2d v3 it would need to be:
要将 CCSprite 的图像更改为每帧之间间隔 1 秒的动画:
To change the image of a CCSprite as an animation with 1 second between each frame:
考虑一个名为 mySprite 的 CCSprite 对象。现在您可以按如下方式更改精灵的图像:
这会将 CCSprite 对象 mySprite 的图像更改为 myNewImage.png
注意:如果要更改的图像位于资源的任何特定文件夹中,那么您可以通过以下方式评估该图像:使用图像的整个路径。
Consider a CCSprite object named mySprite. Now you can change the image of the sprite as follows:
This will change the image of the CCSprite object mySprite to myNewImage.png
Note : If the image to be changed is in any particular folder of the assets, then you can assess that image by using the whole path of the image.