Cocos2d – 将一个精灵从A点移动到B点,以正弦波运动

什么是最好的方法来做到这一点? 我看到了CCEaseSineInOut动作,但看起来不像是可以用来做到这一点。

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

我总是喜欢完全控制CCNode运动。 我只使用CCAction来做非常基本的事情。 虽然你的情况听起来很简单,可能与CCAction ,但我会告诉你如何根据任何function移动一个CCNode随着时间的推移。 您也可以使用相同的技术更改比例,颜色,不透明度,旋转angular度甚至锚点。

 @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 // ie) 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 

对于你的具体问题,你可以用x(t) = k * t + cy(t) = A * sin(w * t) + d来使用选项2。

math笔记#1: x(t)y(t)称为位置参数化。 Vx(t)Vy(t)是速度参数化。

math笔记#2:如果你已经学习了微积分,很容易看出选项2可以防止位置误差随时间积累(特别是对于低帧速率)。 如有可能,请使用选项2.但是,当精度不是问题或用户input正在改变参数设置时,使用选项1通常更容易。

使用CCAction有很多优点。 他们在特定的时间处理您的其他function。 他们保持跟踪,以便您可以轻松地暂停他们,并重新启动或计数。

但是当你真的需要pipe理节点时,这是做这件事的方法。 例如,对于位置复杂或错综复杂的公式,改变参数化要比在CCAction实现参数化要容易得多。

Interesting Posts