actionscript 3 - AS3: repeating function in sigle frame timeline -
i'm new in action script. have single frame timeline , there function moves movie clip verticaly. want repeat 3 times. code works, i'm not sure if correct way or if it's complicated.
var pocet:number = 0; pruh.addeventlistener(event.enter_frame, fl_animatevertically); function fl_animatevertically(event:event) { if (pruh.y >= stage.stageheight) { pocet++; } if (pruh.y < stage.stageheight) { pruh.y += 3; } else { pruh.y = 0 - pruh.y; } if (pocet == 3) { pruh.removeeventlistener(event.enter_frame, fl_animatevertically); } }
thanx
congratulations on achieving goal.
your code improved in terms of readability. have fl_animatevertically
descriptive name, other it's kind of hard figure out what's going on exactly. mean sure adds 3 y results in movement, it's not trivial understand exact behaviour.
that's why want use abstraction or more of top down approach called.. doing @ moment adding value coordinate, result creates animation. want create animation, without going details means.
and sure enough, people created animations code before. that's why can create animation in abstract sense: animation change of property of object on time. in realm of flash animation called tween , there's class doing that..
let's take example code there:
var mytween:tween = new tween(myobject, "x", elastic.easeout, 0, 300, 3, true);
and apply situation.
var verticalanimation:tween = new tween(pruh, "y", elastic.easeout, pruh.y, stage.stageheight, 3, true);
you have adjust duration liking. hope see how easier read , maintain, because specify properties of animation duration. can specify easing, makes motion more interesting.
ok, 1 animation, want 3, right? more precisely, want same animation again, when finished. , can that:
var animationcount:uint = 0; var verticalanimation:tween = new tween(pruh, "y", elastic.easeout, pruh.y, stage.stageheight, 3, true); verticalanimation.addeventlistener(tweenevent.motion_finish, onmotionfinish); // wait animation finished function onmotionfinish(e:tweenevent):void { animationcount++; // add 1 counter if(animationcount >= 3) // check how many times animation finished far { // if last one, remove listener verticalanimation.removeeventlistener(tweenevent.motion_finish, onmotionfinish); } else { // otherwise rewind , start again verticalanimation.rewind(); verticalanimation.start(); } }
there other libraries built in tween
class far more powerful. the 1 greensock popular , easy use can find documentation flash version here
Comments
Post a Comment