ios - Cocos2d - move a sprite from point A to point B in a sine wave motion -
what best way this? see cceasesineinout action doesn't used this.
i need move 1 side of screen other. sprite should move in sine-wave pattern across screen.
i have complete control on ccnode
motion. use ccaction
s basic things. while case sounds simple enough possibly ccaction
s, show how move ccnode
according function on time. can change scale, color, opacity, rotation, , anchor point 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 @ framerate // exact time between calls passed selector [self schedule:@selector(updatenodeproperties:)]; } -(void)updatenodeproperties:(cctime)dt { t += dt; // option 1: update properties "differentially" cgpoint velocity = ccp( vx(t), vy(t) ); // have provide vx(t), , 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) ); // have provide x(t) , y(t) nodetomove.rotation = ... nodetomove.scale = ... ... // in either case, may want stop action if condition met // i.e.) if(nodetomove.position.x > [[ccdirector shareddirector] winsize].width){ [self unschedule:@selector(updatenodeproperties:)]; // perhaps want call other method [self calltosomemethod]; } } @end
for specific problem, use option 2 x(t) = k * t + c
, , y(t) = * sin(w * t) + d
.
math note #1: x(t)
, y(t)
called position parameterizations. vx(t)
, vy(t)
velocity parameterizations.
math note #2: if have studied calculus, readily apparent option 2 prevents accumulation of positional errors on time (especially low framerates). when possible, use option 2. however, easier use option 1 when accuracy not concern or when user input actively changing parameterizations.
there many advantages using ccaction
s. handle calling other functions @ specific times you. kept track of can pause them , restart them, or count them.
but when need manage nodes generally, way it. complex or intricate formulas position, example, easier change parameterizations figure out how implement parameterization in ccaction
s.
Comments
Post a Comment