如何在cocos2D中添加多个精灵表并使其看起来像一个精灵?

Sha*_*waz 1 objective-c cocos2d-iphone

请帮助我任何可能的人.我正在构建一个游戏,我将加载多个CCSpriteBatchNode对象并使它们更改坐标并旋转框架,这样看起来好像它们是动画的并且它们正在移动.我已经实现了将一个CCSpriteBatchNode对象从一个坐标移动到另一个坐标并且它是动画的.现在我需要它做另一个非常不同的动画并加载另一个精灵表文件并移动到其他地方,我该怎么办呢?

到目前为止这是我的代码:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"PotkaEntry.plist"];

CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"PotkaEntry.pvr.ccz"];

[self addChild:spriteSheet];

NSMutableArray *entryAnimFrames = [NSMutableArray array];

for(int i = 1; i<=12; i++)
{
    [entryAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Potka_entry%d.png", i]]];
}

CCAnimationCache *entryAnim = [CCAnimation animationWithFrames:entryAnimFrames delay:0.08f];

CGSize winSize = [CCDirector sharedDirector].winSize;

self->_body1 = [CCSprite spriteWithSpriteFrameName:@"Potka_entry1.png"];

_body1.position = CGPointMake(winSize.width/2, 0);

self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:entryAnim restoreOriginalFrame:NO]];

[_body1 runAction:_walkAction];

_body1.scale = 0.4;

[spriteSheet addChild:_body1];

id entryAction = [CCMoveTo actionWithDuration:5.0f position:ccp(winSize.width/2,60)];

[_body1 runAction:entryAction];
Run Code Online (Sandbox Code Playgroud)

FBr*_*t87 5

您需要为您使用的每个spritesheet创建一个新的CCSpriteBatchNode(通过spritesheet我的意思是组合的pvr.ccz文件和.plist文件)

CCSpriteFrameCache是​​在所有场景和类之间共享的单个缓存.当你调用这个方法时:

[CCSpriteFrameCache sharedSpriteFrameCache]
Run Code Online (Sandbox Code Playgroud)

您不是每次都创建一个新的CCSpriteFrameCache对象,只有一个实例.您将所有已加载的spritesheets存储在此单个缓存中.所以你可以将2个spritesheets加载到缓存中,如下所示:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"sheet1.plist"]; 
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"sheet2.plist"];
Run Code Online (Sandbox Code Playgroud)

然后,您需要为EACH spritesheet创建CCSpriteBatchNode,批处理节点中不能有多个工作表:

CCSpriteBatchNode *spriteSheet1 = [CCSpriteBatchNode batchNodeWithFile:@"sheet1.pvr.ccz"]; 
CCSpriteBatchNode *spriteSheet2 = [CCSpriteBatchNode batchNodeWithFile:@"sheet2.pvr.ccz"];
Run Code Online (Sandbox Code Playgroud)

然后,您可以根据需要将这两个批处理节点添加到图层.添加到批处理节点的Sprite必须来自批处理节点正在使用的spritesheet.