断言失败尝试在cocos2d iphone中停止精灵操作时出错

时间:2013-02-12 06:49:42

标签: iphone ios objective-c cocos2d-iphone

我在停止行动方面遇到了问题。我有2个精灵动画1是蚂蚁,第二个是蚱蜢。当我刚刚调用蚂蚁动画它工作正常,但当我尝试运行蚂蚁和蚱蜢动画它给我错误。 * 断言失败 - [CCSpriteBatchNode addChild:z:tag:],/ Users /zohaib / Download / zhaibgame / zhabogame / 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]; 
}

1 个答案:

答案 0 :(得分:0)

我弄清楚问题是什么。 当我为ant创建动画时我正在使用此代码

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。自我和自我。我创建了一个新的plist文件,并将png名称从1.png更改为ant1.png。它现在有效。