如何在Cocos2d中停止以前运行的动画

时间:2011-10-05 13:01:52

标签: iphone objective-c cocos2d-iphone

我正在使用cocos2d进行游戏。

我通过在 ccTouchBegan 方法中应用CCRepeatForever动作来移动精灵,当精灵与另一个精灵碰撞时,碰撞动画位于 ccTouchMoved 方法中.I'在碰撞结束时无法停止动画 - 我正在使用 stopAction 停止它,但它无法正常工作。

这是我正在使用的代码:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {    
    [selSprite resumeSchedulerAndActions];
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
    [self selectSpriteForTouch:touchLocation];   
    return TRUE;    
}

- (void)selectSpriteForTouch:(CGPoint)touchLocation 
{
    else if(selSprite==FuelTruck) 
    {
        NSMutableArray *FuelTruckWalkAnimFrames = [NSMutableArray array];
        for(int i = FuelTruckTouchStartFrameIndex; i <= FuelTruckTouchEndFrameIndex; ++i) {
            [FuelTruckWalkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"ftruck_move_%d.png", i]]];
        }
        FuelTruckWalkAnim = [CCAnimation animationWithFrames:FuelTruckWalkAnimFrames delay:FuelTruckTouchFrameDelay];
        walkFuelTruck = [CCRepeatForever actionWithAction:
                            [CCAnimate actionWithAnimation:FuelTruckWalkAnim restoreOriginalFrame:YES]];
        [selSprite runAction:walkFuelTruck]; 
    }
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {    

    else if(CGRectIntersectsRect(selSprite.boundingBox,BluePlaneFuelHatch.boundingBox))
    { 
        NSMutableArray *FuelTruckAnimFrames = [NSMutableArray array];
        for(int i = FuelTruckCollisionStartFrameIndex; i <= FuelTruckCollisionEndFrameIndex; ++i) 
        {
            [FuelTruckAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"ftruck_col_%d.png", i]]];
        }
        CCAnimation *FuelTruckAnim = [CCAnimation animationWithFrames:FuelTruckAnimFrames delay:FuelTruckCollisionFrameDelay];

        CCAction *FuelTruckCollisionBlue = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:FuelTruckAnim restoreOriginalFrame:NO]];

        [FuelTruck runAction:FuelTruckCollisionBlue];

        isCollided=YES;
    }
    else
    {

        if(isCollided==YES)
        { 
            isCollided=No;
            [FuelTruck stopAction:FuelTruckCollisionBlue];
        }
    }
}        

但碰撞结束后,这并不能阻止碰撞动画。完成动画需要几毫秒,为什么会这样呢?

1 个答案:

答案 0 :(得分:1)

除了您的代码无法编译之外,它似乎是一个范围问题。看看我在你的代码的删节版本中添加的评论。

    if(CGRectIntersectsRect(selSprite.boundingBox,BluePlaneFuelHatch.boundingBox))
    { //<(1)>
        ...
        //THIS ANIMATION IS ONLY AVAILABLE WITHIN THE BRACES MARKED WITH (1)
        CCAnimation *FuelTruckAnim = [CCAnimation animationWithFrames:FuelTruckAnimFrames delay:FuelTruckCollisionFrameDelay];

        CCAction *FuelTruckCollisionBlue = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:FuelTruckAnim restoreOriginalFrame:NO]];
        ...
        isCollided=YES;
    }//<(1)>
    else
    {

        if(isCollided==YES)
        { 
            isCollided=No;
            //YOU ARE THEN TRYING TO USE THE VARIABLE HERE. IT DOESN'T EXIST IN THIS SCOPE.
            [FuelTruck stopAction:FuelTruckCollisionBlue];
        }
    }

请改为尝试:

    //As a member variable
    CCAction *FuelTruckCollisionBlue;




    if(CGRectIntersectsRect(selSprite.boundingBox,BluePlaneFuelHatch.boundingBox))
    { //<(1)>
        ...
        //THIS ANIMATION IS ONLY AVAILABLE WITHIN THE BRACES MARKED WITH (1)
        CCAnimation *FuelTruckAnim = [CCAnimation animationWithFrames:FuelTruckAnimFrames delay:FuelTruckCollisionFrameDelay];

        FuelTruckCollisonBlue = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:FuelTruckAnim restoreOriginalFrame:NO]];
        ...
        isCollided=YES;
    }//<(1)>
    else
    {

        if(isCollided==YES)
        { 
            isCollided=No;
            //YOU ARE THEN TRYING TO USE THE VARIABLE HERE. IT DOESN'T EXIST IN THIS SCOPE.
            [FuelTruck stopAction:FuelTruckCollisionBlue];
        }
    }