Cocos2D 我应该为每个精灵使用一张图像
如果屏幕上同时有多个相同的精灵,我是否需要为每个精灵加载一张图像,或者我可以简单地以某种方式重新使用这些图像吗?
即,
我需要这样做吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的解决方案很好。第一次调用 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.
您应该正确使用
参考
此方法使用图像或由键命名的缓存纹理。如果没有可用的缓存纹理,它将创建一个纹理并为您缓存。
You should propably use
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.