如何切换CCSprite的图像

发布于 2024-12-16 18:02:48 字数 745 浏览 1 评论 0原文

我有一个使用 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最舍不得你 2024-12-23 18:02:48

救援 API 参考!

当你有一个带有精灵框架的纹理时,您不想更改纹理,而是更改精灵使用的精灵框架。你可以这样做:

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

在 cocos2d v3 中,它需要是:

sprite.spriteFrame = frame;

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:

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

in cocos2d v3 it would need to be:

sprite.spriteFrame = frame;
多像笑话 2024-12-23 18:02:48

要将 CCSprite 的图像更改为每帧之间间隔 1 秒的动画:

CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];

CCSpriteFrame *frame1 = [cache spriteFrameByName:[NSString stringWithFormat:@"plist_file_key_here1.png"]];               
CCSpriteFrame *frame2 = [cache spriteFrameByName:[NSString stringWithFormat:@"plist_file_key_here2.png"]];             

NSArray *animFrames = [NSArray arrayWithObjects:frame1, frame2, nil];

CCAnimation *animation = [CCAnimation animationWithFrames:animFrames delay:1.0f];
[originalSprite runAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]];

To change the image of a CCSprite as an animation with 1 second between each frame:

CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];

CCSpriteFrame *frame1 = [cache spriteFrameByName:[NSString stringWithFormat:@"plist_file_key_here1.png"]];               
CCSpriteFrame *frame2 = [cache spriteFrameByName:[NSString stringWithFormat:@"plist_file_key_here2.png"]];             

NSArray *animFrames = [NSArray arrayWithObjects:frame1, frame2, nil];

CCAnimation *animation = [CCAnimation animationWithFrames:animFrames delay:1.0f];
[originalSprite runAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]];
墟烟 2024-12-23 18:02:48

考虑一个名为 mySprite 的 CCSprite 对象。现在您可以按如下方式更改精灵的图像:

[mySprite setTexture:[[CCTextureCache sharedTextureCache] addImage:[Tools imageNameForName:"myNewImage.png"]]];

这会将 CCSprite 对象 mySprite 的图像更改为 myNewImage.png

注意:如果要更改的图像位于资源的任何特定文件夹中,那么您可以通过以下方式评估该图像:使用图像的整个路径。

Consider a CCSprite object named mySprite. Now you can change the image of the sprite as follows:

[mySprite setTexture:[[CCTextureCache sharedTextureCache] addImage:[Tools imageNameForName:"myNewImage.png"]]];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文