Cocos2d-以正弦波运动将精灵从A点移动到B点 [英] Cocos2d - move a sprite from point A to point B in a sine wave motion

查看:145
本文介绍了Cocos2d-以正弦波运动将精灵从A点移动到B点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做到这一点的最佳方法是什么?我看到了CCEaseSineInOut动作,但看起来好像不能用来执行此操作.

What would be the best way to do this? I see the CCEaseSineInOut action but it doesn't look like that could be used to do this.

我需要从屏幕的一侧移到另一侧.精灵应该在屏幕上以正弦波模式移动.

I need to move from one side of the screen to the other. The sprite should move in a sine-wave pattern across the screen.

推荐答案

我一直希望完全控制CCNode运动.我只使用CCAction做非常基本的事情.尽管您的案例听起来很简单,可以使用CCAction进行处理,但我将向您展示如何随着时间的推移根据任何功能移动CCNode.您还可以使用相同的技术来更改比例,颜色,不透明度,旋转甚至锚点.

I always like to have complete control over CCNode motion. I only use CCActions to do very basic things. While your case sounds simple enough to possibly do with CCActions, I will show you how to move a CCNode according to any function over time. You can also change scale, color, opacity, rotation, and even anchor point with the same technique.

@interface SomeLayer : CCLayer
{
    CCNode *nodeToMove;
    float t; // time elapsed
}
@end

@implementation SomeLayer

// Assumes nodeToMove has been created somewhere else
-(void)startAction
{
    t = 0;

    // updateNodeProperties: gets called at the framerate
    // The exact time between calls is passed to the selector
    [self schedule:@selector(updateNodeProperties:)];
}

-(void)updateNodeProperties:(ccTime)dt
{
    t += dt;

    // Option 1: Update properties "differentially"
    CGPoint velocity = ccp( Vx(t), Vy(t) ); // You have to provide Vx(t), and Vy(t)
    nodeToMove.position = ccpAdd(nodeToMove.position, ccpMult(velocity, dt));
    nodeToMove.rotation = ...
    nodeToMove.scale = ...
    ...

    // Option 2: Update properties non-differentially
    nodeToMove.position = ccp( x(t), y(t) ); // You have to provide x(t) and y(t)
    nodeToMove.rotation = ...
    nodeToMove.scale = ...
    ...

   // In either case, you may want to stop the action if some condition is met
   // i.e.)
   if(nodeToMove.position.x > [[CCDirector sharedDirector] winSize].width){
       [self unschedule:@selector(updateNodeProperties:)];
       // Perhaps you also want to call some other method now
       [self callToSomeMethod];
   }
}

@end

对于您的特定问题,可以将选项2与x(t) = k * t + cy(t) = A * sin(w * t) + d一起使用.

For your specific problem, you could use Option 2 with x(t) = k * t + c, and y(t) = A * sin(w * t) + d.

数学注释1: x(t)y(t)被称为位置参数化. Vx(t)Vy(t)是速度参数化.

Math note #1: x(t) and y(t) are called position parameterizations. Vx(t) and Vy(t) are velocity parameterizations.

数学注释2::如果您已经研究了演算,那么很明显,选项2可以防止位置误差随时间的推移而累积(尤其是对于低帧频).如果可能,请使用选项2.但是,当不关心准确性或用户输入正在主动更改参数设置时,使用选项1通常会更容易.

Math note #2: If you have studied calculus, it will be readily apparent that Option 2 prevents accumulation of positional errors over time (especially for low framerates). When possible, use Option 2. However, it is often easier to use Option 1 when accuracy is not a concern or when user input is actively changing the parameterizations.

使用CCAction有很多优点.他们为您处理在特定时间调用其他功能.他们会被跟踪,以便您可以轻松地暂停它们并重新启动它们,或者对它们进行计数.

There are many advantages to using CCActions. They handle calling other functions at specific times for you. They are kept track of so that you can easily pause them and restart them, or count them.

但是,当您确实确实需要一般地管理节点时,这就是这样做的方法.例如,对于复杂或复杂的位置公式,更改参数设置比弄清楚如何在CCAction s中实现该参数设置要容易得多.

But when you really need to manage nodes generally, this is the way to do it. For complex or intricate formulas for position, for example, it is much easier to change the parameterizations than to figure out how to implement that parameterization in CCActions.

这篇关于Cocos2d-以正弦波运动将精灵从A点移动到B点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆