Ios 如何在cocos2d中的不同CCSpriteBatchNode之间切换精灵

Ios 如何在cocos2d中的不同CCSpriteBatchNode之间切换精灵,ios,cocos2d-iphone,sprite,Ios,Cocos2d Iphone,Sprite,我正在cocos2d中制作一个游戏,我有一个使用CCSprite的小鸡角色。这只鸡有多个图像做不同的动作 有这么多的运动图像,我不得不使用多个plist文件的精灵 我的问题是我可以在不同的精灵之间切换 [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @"chicken-1to3.plist"]; [[CCSpriteFrameCache sharedSpriteFrameCac

我正在cocos2d中制作一个游戏,我有一个使用CCSprite的小鸡角色。这只鸡有多个图像做不同的动作

有这么多的运动图像,我不得不使用多个plist文件的精灵

我的问题是我可以在不同的精灵之间切换

    [[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;
之后我得到了这个错误: “CCSprite未使用相同的纹理id”

更新:我正在使用两种不同的精灵:

    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];
我把它们换成这样:

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

使用两个精灵,每个批处理节点一个。从另一个批处理节点切换到动画时,将动画精灵设置为可见并隐藏另一个。为了使它们同步设置动画,将两个精灵添加到一个ccnode中,该ccnode将代表鸡角色的位置、动作和其他逻辑


如果鸡是批处理节点中唯一的雪碧子节点,您可以简单地删除批处理节点,因为批处理只有在具有相同雪碧和纹理的多个实例时才有帮助。

LearnCos2D,非常感谢!,现在我有一只快乐的舞鸡。当您说“删除批处理节点”时,您介意删除行“[self-addChild:spriteSheet1to3 z:1]”,并使用“[self-addChild:chickenMan1to3];”?以及删除“CCSpriteBatchNode batchNodeWithFile”。是的,根本没有批处理节点。假设每个批处理节点只有一个子节点,那么它就会遇到阻碍。
        [chickenMan1to3 stopAllActions];
        [chickenMan1to3 setVisible:NO];
        [chickenMan4to5 setVisible:YES];
        [chickenMan4to5 runAction:[CCRepeatForever actionWithAction:
                                   [CCAnimate actionWithAnimation:chickenManAni4 restoreOriginalFrame:NO]]];