开发者

How to stop previously running animation in Cocos2d

开发者 https://www.devze.com 2023-04-11 11:53 出处:网络
I\'m making a game using cocos2d. I\'m moving sprites by applying the CCRepeatForever action in ccTouchBegan method and when the sprite collides with another sprite, the collision animation is at in

I'm making a game using cocos2d.

I'm moving sprites by applying the CCRepeatForever action in ccTouchBegan method and when the sprite collides with another sprite, the collision animation is at in ccTouchMoved method.I'm having trouble stopping the animation when the collision is over - I'm stopping it by using stopAction but it is not working.

This is the code I'm using:

- (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:Fuel开发者_JS百科TruckAnim restoreOriginalFrame:NO]];

        [FuelTruck runAction:FuelTruckCollisionBlue];

        isCollided=YES;
    }
    else
    {

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

But this is not stopping collision animation when collision is over. It takes few milliseconds to complete the animation why it is so?


Apart from the fact that your code won't compile, it seems to be a scope issue. Have a look at the comments I've added to this abridged version of your code.

    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];
        }
    }

Try this instead:

    //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];
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号