最好的方法是什么?我看到了CCEaseSineInOut动作,但它看起来不能用来做这件事。
我需要从屏幕的一侧移动到另一侧。精灵应该以正弦波模式在屏幕上移动。
我总是喜欢完全控制CCNode
的运动。我只使用CCAction
来做一些非常基本的事情。虽然您的案例听起来很简单,可能与CCAction
s有关,但我将向您展示如何随着时间的推移根据任何函数移动CCNode
。您也可以使用相同的技术更改比例、颜色、不透明度、旋转,甚至锚点。
@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 + c
和y(t) = A * sin(w * t) + d
一起使用。
数学注释#1:x(t)
和y(t)
称为位置参数化。CCD_ 9和CCD_。
数学注意事项2:如果你学习过微积分,很明显选项2可以防止位置误差随着时间的推移而累积(尤其是对于低帧率)。如果可能,请使用选项2。然而,当精度不是问题时,或者当用户输入正在积极改变参数时,使用选项1通常更容易。
使用CCAction
s有很多优点。它们可以处理在特定时间调用其他函数的问题。它们会被跟踪,这样你就可以很容易地暂停它们并重新启动它们,或者对它们进行计数。
但是,当你真的需要管理节点时,这就是方法。例如,对于复杂或复杂的位置公式,更改参数化要比在CCAction
s中实现参数化容易得多。