Search code examples
cocos2d-iphonescale

How to Keep New Scale Size Cocos2D


Here's what I'm trying to do.

I have a sprite that is constantly scaling down over the period of 60 seconds. This sprite then scales up by 1.5 when collision occurs.

        -(void) update: (ccTime) deltaTime 

        {if(CGRectIntersectsRect([randomSprite boundingBox], [other boundingBox]))  {

        [SAE playEffect:@"collisionSoundEffect.mp3"];
        [randomSprite runAction:[CCScaleBy actionWithDuration:3 scaleX:1.5 scaleY:1.5]];

The problem is when after 3 seconds it 'Pops' back down to the size it would have been as if the collision didn't happen.

Which of course is smaller.

What I want the sprite to do is to stay the newSize after the collision. Then for the sprite to keep scaling down as it was.

Any help or guidance is greatly appreciated.

Thank you.

[thisSpriteIsConstatnlyScalingDown runAction:[CCScaleBy actionWithDuration:60 scaleX:.01 scaleY:.01]];

I'm trying to keep the new scaled size and start scaling down from that again.


Solution

  • I think it's because you have two actions running at the same time. After scale up action finishes, scale down action is still running.

    #define kScaleDownActionTag 1
    #define kScaleUpActionTag 2
    
    
        ...
    
        // start downscale action
        CCAction *action = /* your scale down action */;
        action.tag = kScaleDownActionTag;
        [sprite runAction:action];
    
    
        ...
    
        if(CGRectIntersectsRect([randomSprite boundingBox], [other boundingBox]))  
        {
            // if scale up action isn't running already
            if (![randomSprite getActionByTag:kScaleUpActionTag])
            {
                // stop downscale action and run upscale action
                [randomSprite stopActionByTag:kScaleDownActionTag];
                CCAction *action = /* your scale up action */
                action.tag = kScaleUpActionTag;
                [randomSprite runAction:action];
            }
        }