【问题标题】:Cocos2d and iOS: how to use UIBezierPath to animate CCSprite movements?Cocos2d 和 iOS:如何使用 UIBezierPath 动画 CCSprite 动作?
【发布时间】:2012-09-14 10:05:32
【问题描述】:

我发现 several post 教如何将 UIBezierPaths 与 CoreAnimation 一起使用。这是因为 CAKeyframeAnimation 对象有一个名为 path 的属性,它的类型是 CGPath,而且 UIBezierPaths 对象也有一个 CGPath 属性。

我想知道如何让 CCSprite 跟随 CGPath 移动。

在 CGPath 中有一个函数 CGPathGetCurrentPoint 可以帮助我获取路径中的当前点,但我找不到“移动到下一个点”之类的东西。

我的伪代码会这样工作:

Create a CCSprite subclass with a UIBezierPaths property
Initialize the UIBezierPaths instance

Allocate the CCSprite subclass in a layer
schedule updates
In update method
     Reposition the CCSprite sublcass instance by getting the CGPathGetCurrentPoint to CGPathGetCurrentPoint
     Move the path forward by calling "move to next point"

知道这是否可行吗?这是一种有效的方法吗?

【问题讨论】:

    标签: cocos2d-iphone sprite uibezierpath ccsprite cgpath


    【解决方案1】:

    不确定您要做什么,但这就是我在 Cocos2D 中沿贝塞尔路径移动一些精灵的方式:

    - (CCBezierTo*)bezierMoveTo:(CGPoint)destination node:(CCNode*)node duration:(float)d {
    
     static ccBezierConfig bzPaths[5] = {
        {{1050, 900}, {1200, 1400}, {1100, 900}},
        {{1100, 750}, {1300, 1550}, {1650, 500}},
        {{800, 850}, {500, 1100}, {400, 700}},
        {{700, 700}, {1500, 1200}, {400, 900}},
        {{900, 650}, {100, 900}, {900, 400}}
     };
     ccBezierConfig bezierOne;
     bezierOne = bzPaths[PATH_INDEX];
     return [CCBezierTo actionWithDuration:d bezier:bezierOne];
    }
    

    然后可以在精灵上运行返回的动作。就我而言,我有一组静态路径,我随机选择一个。在您的情况下,您似乎希望将路径存储在自定义精灵中。除此之外,一切都应该正常。

    如果您想构建更复杂的路径,只需使用ccBezierConfigCCBezierBy 的序列即可。

    【讨论】:

    • 非常感谢。我已经验证 ccBezierConfig 只有 2 个控制点和 1 个端点。有没有办法使用从 UIBezierPaths 对象派生的 ccBezierConfig 来制作更复杂的路径?我的主题不清楚,所以我可能会说一些愚蠢的东西。但在我看来,ccBezierConfig 只是一个子路径的配置,而 UIBezierPaths 定义了一个更复杂的对象。这有意义吗?如果你想做复杂的动作,你会混合不同的 ccBezierConfig 吗?谢谢:)..
    • PS:一旦我对此有所了解(从 UIBezierPaths 开始),将再次接受答案,因为这是我最初的问题,我需要知道是否可能。如果不可能,我会再次接受答案。对此感到抱歉
    • 请看我的编辑:您可以使用CCBezierBy 通过指定ccBezierConfig 的序列来构建复杂路径。
    • 感谢 Sergio,再次抱歉不接受它。我只是想确定这将是正确的方法:)。对我来说非常有用的输入。非常感谢!!
    • 我和他们玩了一点,但我不能安静地理解控制点与起始位置的用法。我在这里添加了一个问题stackoverflow.com/questions/12430651/…,甚至制作了一个YouTube小视频youtube.com/watch?v=IQ_Jf0O0KGc有什么建议:)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多