开发者

Cocos2D getting progress of CCAction

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 o开发者_Python百科f 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?


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.


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+).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜