Cocos2D 我应该为每个精灵使用一张图像

发布于 2024-10-09 07:56:26 字数 274 浏览 0 评论 0原文

如果屏幕上同时有多个相同的精灵,我是否需要为每个精灵加载一张图像,或者我可以简单地以某种方式重新使用这些图像吗?

即,

我需要这样做吗?

CCSprite *mySprite1 = [CCSprite spriteWithFile:@"mySprite.png"];
CCSprite *mySprite2 = [CCSprite spriteWithFile:@"mySprite.png"];

或者有更好的方法来处理相同的精灵吗?

If I have several identical sprites on the screen at once, do I need to load one image for each of the sprite, or can I simply re-use the images somehow?

i.e,

Do I need to do this?

CCSprite *mySprite1 = [CCSprite spriteWithFile:@"mySprite.png"];
CCSprite *mySprite2 = [CCSprite spriteWithFile:@"mySprite.png"];

Or is there a better way to deal with identical sprites?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

玩世 2024-10-16 07:56:26

你的解决方案很好。第一次调用 spriteWithFile: 将加载纹理并将其添加到共享的 CCTextureCache 中。第二个将检查纹理是否已存在,这次将重用第一个纹理。

Your solution is fine. The first call to spriteWithFile: will load the texture and add it to the shared CCTextureCache. The second one will check to see if the texture already exists, and this time will reuse it from the first one.

音盲 2024-10-16 07:56:26

您应该正确使用

- (id)initWithCGImage:(CGImageRef)image
                  key:(NSString *)key   

参考

此方法使用图像或由键命名的缓存纹理。如果没有可用的缓存纹理,它将创建一个纹理并为您缓存。

You should propably use

- (id)initWithCGImage:(CGImageRef)image
                  key:(NSString *)key   

Reference

This method uses either image or a cached texture named by key. If no cached texture with this key is available, it'll create a texture and cache it for you.

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