开发者

Using cocos2D how do you create an animation using sprites from two sprite sheets?

I have some sprites of stars twinkling and the images are too big to fit on one sprite sheet, so I have had to spread them across two. I have been looking for how to create a single animation using the images from both sprite sheets, but have had no luck.

This is what I tried, it complies ok but then crashes when the animation gets to where the images start coming from the second sprite sheet.

        //==========================================================开发者_如何学编程=============================// 
    //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];

Any ideas where I am going wrong? Thanks in advance.

Hi @JorisMans, I tried your method and it seems to work up till the point where it tries to add the animation to the second sequence. I have tried running the methods in reverse order(so starAnimation_02 first) and it still gives the same out come. The first method runs normally then when the second method to be called runs it crashes. Here are my two methods, I have kept the CCLOG in one of them so you understand where it crashes. The program reaches the REACHED3 CCLOG before crashing.

-(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]; 
}


Every node has its own spritesheet. You cannot apply spritesheet images from one node to another.

One possible solution (sorry for not giving code but maybe you can figure it out):

  1. Create 2 CCAnimations, one for each spritesheet
  2. Add the first star node and second star node as a childnodes of "self"
  3. hide the second star node
  4. Apply an action sequence to the first star node where you play the animation of the sprites of your first spritesheet, without repeat, and at the end of your animation you do a custom callback
  5. In your custom callback you hide the first star node, unhide the second one
  6. Apply an action sequence to the second node where you play the animation of the sprites of your second spritesheet, without repeat, and at the end of your animation you do a custom callback
  7. In your custom callback you hide the second star node, unhide the first one
  8. Go to step 4

Hope this helps you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜