1

I'm making a game in cocos2d, I have a chicken as character that use a CCSprite. This chicken have multiple images doing different movements.

There are so many images for the movements that I have to use multiple plist files for the sprite.

My problem is that I can switch between the differents sprites

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
     @"chicken-1to3.plist"];

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
     @"chicken-4to5.plist"];

    spriteSheet1to3 = [CCSpriteBatchNode
                                      batchNodeWithFile:@"chicken-1to3.png"];
    spriteSheet4to5 = [CCSpriteBatchNode
                                      batchNodeWithFile:@"chicken-4to5.png"];
    [self addChild:spriteSheet1to3 z:1];
    [self addChild:spriteSheet4to5 z:2];

    NSMutableArray *chickenManAni1Imgs = [NSMutableArray array];
    NSMutableArray *chickenManAni2Imgs = [NSMutableArray array];
    NSMutableArray *chickenManAni3Imgs = [NSMutableArray array];
    NSMutableArray *chickenManAni4Imgs = [NSMutableArray array];
    NSMutableArray *chickenManAni5Imgs = [NSMutableArray array];

    for(int i = 1; i <= 9; ++i) {
        [chickenManAni1Imgs addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"chicken-1-%d.png", i]]];
    }
    for(int i = 1; i <= 19; ++i) {
        [chickenManAni2Imgs addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"chicken-2-%d.png", i]]];
    }
    for(int i = 1; i <= 21; ++i) {
        [chickenManAni3Imgs addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"chicken-3-%d.png", i]]];
    }
    for(int i = 1; i <= 16; ++i) {
        [chickenManAni4Imgs addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"chicken-4-%d.png", i]]];
    }
    for(int i = 1; i <= 36; ++i) {
        [chickenManAni5Imgs addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"chicken-5-%d.png", i]]];
    }

    chickenManAni1 = [CCAnimation
                   animationWithSpriteFrames:chickenManAni1Imgs delay:0.04f];
    chickenManAni2 = [CCAnimation
                      animationWithSpriteFrames:chickenManAni2Imgs delay:0.04f];
    chickenManAni3 = [CCAnimation
                      animationWithSpriteFrames:chickenManAni3Imgs delay:0.04f];
    chickenManAni4 = [CCAnimation
                      animationWithSpriteFrames:chickenManAni4Imgs delay:0.04f];
    chickenManAni5 = [CCAnimation
                      animationWithSpriteFrames:chickenManAni5Imgs delay:0.04f];

    chickenMan = [CCSprite spriteWithSpriteFrameName:@"chicken-5-1.png"];
    chickenMan.position = ccp(winSize.width/2, winSize.height/2);

    [spriteSheet4to5 addChild:chickenMan];

    [chickenMan runAction:[CCRepeatForever actionWithAction:
                          [CCAnimate actionWithAnimation:chickenManAni5 restoreOriginalFrame:NO]]];
    chickenManAniRunning = 1;

Here is the code when I try to change the sprite

        [chickenMan stopAllActions];
        [spriteSheet4to5 removeChild:chickenMan cleanup:YES];
        [spriteSheet1to3 addChild:chickenMan];

        [chickenMan runAction:[CCRepeatForever actionWithAction:
                               [CCAnimate actionWithAnimation:chickenManAni2 restoreOriginalFrame:NO]]];

After that I get this error: 'CCSprite is not using the same texture id'

UPDATE: I'm using two differents sprites:

    chickenMan1to3 = [CCSprite spriteWithSpriteFrameName:@"chicken-1-1.png"];
    chickenMan1to3.position = ccp(winSize.width/2, winSize.height/2);
    [chickenMan1to3 setVisible:YES];

    chickenMan4to5 = [CCSprite spriteWithSpriteFrameName:@"chicken-5-1.png"];
    chickenMan4to5.position = ccp(winSize.width/2, winSize.height/2);
    [chickenMan4to5 setVisible:NO];

And I switch them this way:

        [chickenMan1to3 stopAllActions];
        [chickenMan1to3 setVisible:NO];
        [chickenMan4to5 setVisible:YES];
        [chickenMan4to5 runAction:[CCRepeatForever actionWithAction:
                                   [CCAnimate actionWithAnimation:chickenManAni4 restoreOriginalFrame:NO]]];
4

1 に答える 1

3

各バッチ ノードに 1 つずつ、合計 2 つのスプライトを使用します。他のバッチ ノードからアニメーションに切り替える場合は、アニメーション スプライトを表示し、もう一方を非表示にします。それらを同期してアニメーション化するには、両方のスプライトを ccnode に追加します。これは、位置、アクション、およびその他のロジックに関する限り、ニワトリのキャラクターを表します。

ニワトリがバッチ ノード内の唯一のスプライトの子である場合、バッチ ノードを削除するだけで済みます。これは、同じスプライトとテクスチャの複数のインスタンスがある場合にのみバッチ処理が役立つためです。

于 2013-03-29T09:02:50.557 に答える