How to optimize a acceleration / friction function more
Hello !
In few words what the code does:
It starts rotating from a speed of 2 deg and for 5 secs it accelerates to 20 deg per sec.
after that , the speed of rotation stays 20deg for 10 secs and after the 10 secs are over
it gets in the friction function where it slows down back to 2deg a sec.
but ! as I am not that good with optimization and writing clean code I see myself in a need to ask if this is a good code or it can be done even better (more optimized and cleaner).
The code is this :
Box() is a simple box shape ,nothing more
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip
{
protected var _box:Box = new Box();
private var fps:Number = stage.frameRate;
private var step:Number = 20;
private var minStep:Number = 2;
private var time:Number = 5;
private var increment:Number = (step - minStep) / (time * fps);
// 20 - 2 / 5 * 30
// 18 / 150
// 0.12
protected var timePassed:Number = 0
public function Main()
{
_box.x = stage.stageWidth/2 - _box.width/2;
_box.y = stage.stageHeight/2 - _box.height/2;
this.addChild(_box);
stage.addEventListener(Event.ENTER_FRAME, constantMovement);
}
private function friction():void
{
_box.rotation += step;
step -= increment;
step = Math.max(2, step);
}
private function acceleration():void
{
_box.rotation += minStep;
minStep += increment;
minStep = Math.min(minStep, 20);
}
private function constantMovement(event:Event)
{
if(minStep < 20)
{
acceleration()
}
else if(minStep >= 20 && timePassed <= 10)
{
_box.rotation += 20;
timePassed += 10 / (time * fps)
trace("timePassed :" + timePassed)
}
else if (minStep >= 20 && timePassed >= 10)
{
friction();
}
}
}
}
If there is something more it can be done please tell me/ help me.
