【问题标题】:Which APIs should we use to add spritesheets and sprites to a cocos2d game?我们应该使用哪些 API 来将 spritesheets 和 sprites 添加到 cocos2d 游戏中?
【发布时间】:2013-01-05 05:24:58
【问题描述】:
在创建 cocos2d iOS 游戏时,有几种添加 spritesheets 的选项 - CCTextureCache::addImageAsync、CCSpriteFrameCache::addSpriteFramesWithFile 等 - 使用这些不同的方式添加 spritesheets 有什么区别?
同样,加载一个精灵,我们可以调用 CCSprite::spriteWithSpriteFrameName 或 CCSprite::spriteWithFile 或 CCSpriteBatchNode::batchNodeWithTexture 等。使用这些技术有什么区别?
谢谢
阿南德
【问题讨论】:
标签:
cocos2d-iphone
sprite-sheet
【解决方案1】:
加载精灵帧,这也会加载纹理:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"file.plist"];
使用精灵帧:
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:@"frame.png"];
将精灵添加到批处理节点:
CCSpriteBatchNode* batchNode = [CCSpriteBatchNode batchNodeWithTexture:sprite.texture];
[batchNode addChild:sprite];
如果您使用精灵的相同图像文件,batchNodeWithFile 也可以工作。如果 sprite 是用 spriteframe 初始化的,它将是纹理图集图像(即“file.png”)。
只有当你想在另一个线程上加载你的纹理时才需要addImageAsync,通常是为了动画加载屏幕。之后您仍然需要添加精灵帧。
CCSprite spriteWithFile 从单个图像文件创建一个精灵。这些也可以批量处理,但最好使用带有精灵帧的纹理图集。