Question

I have a Cocos2D game with Box2D physics. In my GameScene.mm, I'm working on a method to zoom to a given scale:

-(void) zoomToScale:(float)zoom withDuration:(ccTime)duration
{
    id action = [CCScaleTo actionWithDuration:duration scale:zoom];
    [scrollNode runAction:action];

    currentZoomLevel = zoom;
}

The problem that I'm having is that currentZoomLevel (which is used in the Scene's update() method) is set to the zoom immediately, and isn't gradually adjusted as per the animation. So while the animation is in progress, the currentZoomLevel variable is totally wrong.

I'm trying to figure out a way to have the currentZoomLevel variable match the progress of the animation as it's happening. According to the CCAction API Reference, the CCAction's update method takes a ccTime that's between 0 and 1 based on the progress of the animation (0 is just started, 1 is just finished).

How can I access this ccTime from outside of the action? I want to have something in my Scene's update method like this:

if(animating)
{
    float progress = [action getProgress]; // How do I do this?

    // Do math to update currentZoomLevel based on progress
}

Am I missing something obvious here, or am I going to have to subclass CCScaleTo?

Was it helpful?

Solution

You should be able to access the scale directly as it animates.

instead of float progress = [action getProgress];

try float current_scale = some_node.scale ;

where "some_node" is the thing you're animating/scaling.

OTHER TIPS

Actually, your best bet is to use the new Cocos2D extension "CCLayerPanZoom", which handles all of this marvellously for you! It should be part of any new cocos2D install (v.1.0+).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top