I always like to have full control over the CCNode movement. I use only CCAction to do very simple things. Although your case sounds simple enough to possibly do with CCAction s, I will show you how to move the CCNode according to any function over time. You can also change the scale, color, opacity, rotation, and even the anchor point using 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 // 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
For your specific problem, you can use Option 2 with x(t) = k * t + c and y(t) = A * sin(w * t) + d .
Math Note # 1: x(t) and y(t) are called position parameters. Vx(t) and Vy(t) are parameterization of speed.
Math Note # 2: If you have studied calculus, it will be obvious that Option 2 prevents the accumulation of positional errors over time (especially for low frames). Whenever possible, use option 2. However, it is often easier to use option 1 when accuracy is not a problem or when user input actively changes parameters.
There are many benefits to using CCAction s. They handle calling other functions at a specific time for you. They are tracked, so you can easily pause, restart, or count them.
But when you really need to manage the nodes as a whole, this is the way to do it. For example, for complex or complex formulas for a position, it is much easier to change the parameters than to figure out how to implement this parameterization in CCAction s.