使用cocos2D如何使用两个精灵表中的精灵创建动画?
问题描述:
我有一些星星闪烁的星云,图像太大而无法放在一张雪碧纸上,所以我不得不将它们分散到两张。我一直在寻找如何使用来自两个精灵表单的图像创建单个动画,但没有运气。使用cocos2D如何使用两个精灵表中的精灵创建动画?
这是我试过的,它符合要求,但是当动画到达图像从第二个精灵表开始的位置时会崩溃。
//=======================================================================================//
//Load Stars
frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:@"starsOne_01.plist"];
[frameCache addSpriteFramesWithFile:@"starsOne_02.plist"];
starSpriteNodeOne_01 = [CCSpriteBatchNode batchNodeWithFile:@"starsOne_01.png"];
[self addChild:starSpriteNodeOne_01 z:-2];
starSpriteNodeOne_02 = [CCSpriteBatchNode batchNodeWithFile:@"starsOne_02.png"];
[self addChild:starSpriteNodeOne_02 z:-2];
//Load frames
starOneFrames_01 = [NSMutableArray array];
for(int i = 1; i <= 12; ++i) {
[starOneFrames_01 addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"starsOne_%d.png", i]]];
}
starOneAnim_01 = [CCAnimation animationWithFrames:starOneAnim_01 delay:0.5f];
[[CCAnimationCache sharedAnimationCache] addAnimation:starOneAnim_01 name:@"starOneAnim_01"];
//=======================================================================================//
//Add stars to the scene
starsOne = [CCSprite spriteWithSpriteFrameName:@"starsOne_1.png"];
starsOne.anchorPoint = ccp(0, 0.5);
starsOne.position = ccp(0, size.height/2);
[starSpriteNodeOne_01 addChild:starsOne];
//animate
starOneAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:starOneAnim_01 restoreOriginalFrame:YES]];
[starsOne runAction:starOneAction];
任何想法,我哪里错了?提前致谢。
嗨@JorisMans, 我试过你的方法,它似乎一直工作,直到它试图将动画添加到第二个序列。我试着以相反的顺序运行这些方法(所以starAnimation_02首先),它仍然给出了相同的结果。第一个方法正常运行,然后第二个要调用的方法运行时崩溃。以下是我的两种方法,我已将CCLOG保存在其中一个中,以便了解其崩溃的位置。程序在崩溃之前达到REACHED3 CCLOG。
-(void)starAnimation_01
{
[starsOne_01 setVisible:YES];
[starsOne_02 setVisible:NO];
CCCallFunc* customCall = [CCCallFunc actionWithTarget:self selector:@selector(starAnimation_02)];
CCSequence* actionSeq = [CCSequence actions:[CCAnimate actionWithAnimation:starOneAnim_01 restoreOriginalFrame:NO],customCall,nil];
[starsOne_01 runAction:actionSeq];
}
-(void)starAnimation_02
{
[starsOne_02 setVisible:YES];
CCLOG(@"===================REACHED========================");
[starsOne_01 setVisible:NO];
CCLOG(@"===================REACHED2========================");
CCCallFunc* customCall2 = [CCCallFunc actionWithTarget:self selector:@selector(starAnimation_01)];
CCLOG(@"===================REACHED3========================");
CCSequence* actionSeq2 = [CCSequence actions:[CCAnimate actionWithAnimation:starOneAnim_02 restoreOriginalFrame:NO],customCall2,nil];
CCLOG(@"===================REACHED4========================");
[starsOne_02 runAction:actionSeq2];
}
答
每个节点都有自己的spritesheet。您无法将spritesheet图像从一个节点应用到另一个节点。
一个可能的解决方案(对不起,不给代码,但也许你自己看着办吧):
- 创建2个CCAnimations,每个spritesheet
- 添加第一个星点和第二星级节点作为“自我”
- 的的childNodes隐藏第二星点
- 应用的操作顺序,你玩 动画您 第一spritesheet的精灵的 第一球星节点,无重复, 在动画的最后,你 做一个定制的回调
- 在您的自定义回调你隐藏 第一球星节点,取消隐藏第二 一个
- 应用的动作顺序的 第二个节点你玩的 动画您 第二spritesheet的精灵,没有重复, 在动画的最后,你 做一个定制的回调
- 在您的自定义回调你隐藏 第二颗星节点,取消隐藏第一 一个
- 转到步骤4
希望这有助于你。
请问您可以举一个例子说明如何将一个自定义回调“附加”回到动画结尾? – 2011-05-22 14:25:23
CCCallFunc * customCall = [CCCallFunc actionWithTarget:self selector:@selector(myMethod)]; CCSequence动作:[CCAnimate actionWithAnimation:currentAnimation restoreOriginalFrame:NO],customCall,nil]; – 2011-05-22 14:26:40
我一直在尝试实施您的解决方案,但遇到问题,请参阅上面我的问题的编辑。请如果你能帮忙? – 2011-05-22 20:11:15