ios - Varying Vertical Position of Sprite During Movement -
i have sprites in ios application move across screen horizontally, both left right , right left. of these sprites, straight line or well-defined arc acceptable. have sprites, however, have move little more randomly, varying vertical position of sprite moves.
in following code, "y_variant_" ivar specifies degree of variance apply. smaller value natural objects birds or man-made objects paper airplanes, should move small amount , down fly, larger value natural objects insects , bees, have greater variance.
this code using (the "update:" method sprite):
// track/vary object moves along path... - ( void ) update: ( cctime ) delta { // ask director window size... cgsize const size = [ [ ccdirector shareddirector ] winsize ]; // current position of projectile... cgpoint const ptraw = [ self position ]; // create variant of vertical position "intelligent" items... cgpoint const pt = ( y_variant_ ? ccp( ptraw.x , ptraw.y + y_variant_ * (float)( rand( ) % 25 ) * 0.02f - fmodf( ptraw.x, y_variant_ ) ) : ptraw ); [ self setposition: pt ]; : : : // if we're supposed stop projectile on impact (a "brick wall", etc.)... if ( fstopprojectile == objectimpactactionstop ) { // object impact, if "smart" projectile (i.e., brain)... if ( y_variant_ ) { // move projectile around object... ccaction * const move = [ ccmoveto actionwithduration: 0.25f position: ccpadd( [ self position ] , ccp( - cs_image_object_w , cs_image_object_h ) ) ]; [ self runaction: move ]; } // end "smart" projectile } // end projectile impact : : : return; } // end update
you'll notice there's logic involving collision solid objects: if solid obstruction (wall, building, etc.) encountered, object backs , moves vertically avoid obstacle.
the problem i'm running viewed motion very erratic larger values of "y_variant_", , appear smoother....
even if don't have solution, comments can improved or investigated appreciated.
thanks help.
p.s.: if you're feeling ambitious, 1 of suggestions user have bee very loop around backwards , continue path. once i've fixed current "bouncing" problem, i'll incorporate that, please keep in mind....
there's no simple answer question. if you're using cocos2d, using actions instead of manually positioning sprites. reiterate, recommend using actions. here's simple example might if stick manual positioning:
#define kamounttomoveperframeiny 10.0 / 60.0 @property float posycurrentlymovingto; @property somespriteclass *thespritemoving; @property float somexposition; @property float nextmoveamount; -(void)dostuffduringupdate { if (_thespritemoving.position.y < labs(_posycurrentlymovingto)) { //sprite still moving position _thespritemoving.position = cgpointmake(_somexposition, _thespritemoving.position.y + _nextmoveamount); }else{ float variationamountiny = arc4random_uniform(200) - 100.0; _posycurrentlymovingto = variationamountiny; _nextmoveamount = variationamountiny * kamounttomoveperframeiny; } }
Comments
Post a Comment