Cocos2D获取CCAction的进度

发布于 2024-11-30 23:44:57 字数 818 浏览 0 评论 0原文

我有一个带有 Box2D 物理原理的 Cocos2D 游戏。在我的 GameScene.mm 中,我正在研究一种缩放到给定比例的方法:

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

    currentZoomLevel = zoom;
}

我遇到的问题是 currentZoomLevel (在场景的 update() 方法中使用)立即设置为缩放,并且并没有按照动画逐步调整。因此,当动画正在进行时,currentZoomLevel 变量是完全错误的。

我正在尝试找出一种方法,让 currentZoomLevel 变量与动画发生的进度相匹配。根据 CCAction API Reference,CCAction 的 update 方法根据动画的进度采用 0 到 1 之间的 ccTime(0 刚刚开始,1 刚刚完成)。

我如何从操作外部访问此 ccTime?我想在场景的更新方法中添加如下内容:

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

    // Do math to update currentZoomLevel based on progress
}

我是否在这里遗漏了一些明显的内容,或者我是否必须子类化 CCScaleTo?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2024-12-07 23:44:57

您应该能够在缩放动画时直接访问它。

而不是
浮动进度 = [action getProgress];

尝试
float current_scale = some_node.scale ;

其中“some_node”是您要设置动画/缩放的对象。

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.

酒几许 2024-12-07 23:44:57

实际上,您最好的选择是使用新的 Cocos2D 扩展“CCLayerPanZoom”,它可以为您出色地处理所有这些!它应该是任何新的 cocos2D 安装(v.1.0+)的一部分。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文