0

我在停止操作时遇到问题。我有 2 个精灵动画 1 是蚂蚁,第二个是蚱蜢。当我只是调用蚂蚁动画时,它工作正常,但是当我尝试同时运行蚂蚁和蚱蜢动画时,它给了我错误。 *断言失败 -[CCSpriteBatchNode addChild:z:tag:], /Users/zohaib/Downloads/zohaibgame/zohaibgame/libs/cocos2d/CCSpriteBatchNode.m:183 调用 antMoveEnded 时会出错。

蚂蚁动画代码

-(void) walk_Ants
{

    // 1) Cache the sprite frames and texture
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
     @"ant-animation.plist"];

    /// 2) Create a sprite batch node
    CCSpriteBatchNode *spriteSheet_ant = [CCSpriteBatchNode
                                          batchNodeWithFile:@"ant-animation.png"];
    [self addChild:spriteSheet_ant];

    // 3rd Step
    // 3) Gather the list of frames
    NSMutableArray *walkAnimFrames = [NSMutableArray array];
    for(int i = 1; i <= 24; ++i) {
        [walkAnimFrames addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"%d.png", i]]];

    }

    // 4th Step
    // 4) Create the animation object
    CCAnimation *walkAnim = [CCAnimation
                             animationWithSpriteFrames:walkAnimFrames delay:0.05f];

    // 5th Step
    // 5) Create the sprite and run the animation action
    self.ants = [CCSprite spriteWithSpriteFrameName:@"1.png"];
    _ants.position = ccp(winSize.width, winSize.height/6);
    //_ants.position = ccp(459, 16);
    self.walkActionAnt = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim]];
    [_ants runAction:_walkActionAnt];
    [spriteSheet_ant addChild:_ants];
    CCLOG(@"Position %@",NSStringFromCGPoint(_ants.position));
    [self walkingAnts];


}

-(void) walkingAnts
{
    // 2) Set the desired velocity
    float antVelocity = 480.0/10.0;

    // 3) Figure out the amount moved in X and Y
    CGPoint waking_Path = CGPointMake(X_AXIS_ENDINGPATH, _ants.position.y);
    CGPoint moveDifference = ccpSub(waking_Path, _ants.position);

    // 4) Figure out the actual length moved
    float distanceToMove = ccpLength(moveDifference);

    // 5) Figure out how long it will take to move
    float moveDuration = distanceToMove / antVelocity;

    // 7) Run the appropriate actions
    //[_ants stopAction:_moveAction];


    id restartAntWalk = [CCCallFuncN actionWithTarget:self selector:@selector(antMoveEnded:)];

    self.moveActionAnt = [CCSequence actions:
                       [CCMoveTo actionWithDuration:moveDuration position:waking_Path],
                       restartAntWalk,
                       nil];
    //self.moveActionAnt.tag = 121;
    [_ants runAction:_moveActionAnt];
    _movingAnt = TRUE;

}

- (void) antMoveEnded: (ccTime) dt
{
    if(_ants.position.x == -50)
    {
        [_ants stopAction:_moveActionAnt];
        //[self stopActionByTag:121];
        _movingAnt = FALSE;
        [self walk_Ants];
    }
}

GrassHopper 代码(螳螂)

-(void) mantisCome
{
    float w_index = arc4random() % 480;
    //float h_index = arc4random() % 320;

    self.moveActionMantis = [CCSequence actions:
                             [CCMoveTo actionWithDuration:2 position:ccp(w_index, 63)],

                             nil];

    [_mantis runAction:_moveActionMantis];
    [self schedule:@selector(timer:) interval:5];

}

-(void) mantisGo
{
    self.moveActionMantis = [CCSequence actions:
                             [CCMoveTo actionWithDuration:2 position:ccp(-50, 280)],

                             nil];

    [_mantis runAction:_moveActionMantis];
    [self unschedule:@selector(timer:)];
    [self mantisAnimation];
}

#pragma -mark Mantis Animation
-(void) mantisAnimation
{

    // 1) Cache the sprite frames and texture
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
     @"mantis-animation.plist"];

    /// 2) Create a sprite batch node
    CCSpriteBatchNode *spriteSheet_mantis = [CCSpriteBatchNode
                                             batchNodeWithFile:@"mantis-animation.png"];
    [self addChild:spriteSheet_mantis];

    // 3rd Step
    // 3) Gather the list of frames
    NSMutableArray *walkAnimFrames_mantis = [NSMutableArray array];
    for(int i = 1; i <= 14; ++i) {
        [walkAnimFrames_mantis addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"%d.png", i]]];

    }

    // 4th Step
    // 4) Create the animation object
    CCAnimation *walkAnim_mantis = [CCAnimation
                                    animationWithSpriteFrames:walkAnimFrames_mantis delay:0.05f];

    // 5th Step
    // 5) Create the sprite and run the animation action
    self.mantis = [CCSprite spriteWithSpriteFrameName:@"1.png"];
    _mantis.position = ccp(winSize.width+100, winSize.height+100);
    _mantis.flipX=YES;
    //_mantis.position = ccp(459, 16);
    self.walkActionMantis = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim_mantis]];
    walkAnim_mantis.restoreOriginalFrame = NO;
    [_mantis runAction:_walkActionMantis];
    [spriteSheet_mantis addChild:_mantis];
    //CCLOG(@"Position %@",NSStringFromCGPoint(_mantis.position));
    [self mantisCome]; 
}
4

1 回答 1

0

我弄清楚了问题所在。当我为蚂蚁创建动画时,我正在使用这段代码

NSMutableArray *walkAnimFrames = [NSMutableArray array];
    for(int i = 1; i <= 24; ++i) {
        [walkAnimFrames addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"%d.png", i]]];

    }

self.ants = [CCSprite spriteWithSpriteFrameName:@"1.png"];

在为螳螂创建动画时

NSMutableArray *walkAnimFrames_mantis = [NSMutableArray array];
    for(int i = 1; i <= 14; ++i) {
        [walkAnimFrames_mantis addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"%d.png", i]]];

    }

self.mantis = [CCSprite spriteWithSpriteFrameName:@"1.png"];

我对两者都使用了 1.png。self.ant 和 self.mantis。我创建了一个新的 plist 文件并将 png 名称从 1.png 更改为 ant1.png 。现在可以了。

于 2013-02-12T07:27:10.947 回答