Copy link to clipboard
Copied
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.
You can do the following:
1. Replace the last else if (minStep >= 20 && timePassed >= 10) with simply else, as the condition would always be true if control reaches there.
2. Store the value 10 / (time * fps) in a private variable and use that directly, rather than evaluating it each time as the value remains constant.
You can also try replacing the if-else-if block with a switch-case block on a state variable in the class (with values as 0, 1, 2 signifying accelerating, constant speed and decelera
...Copy link to clipboard
Copied
You can do the following:
1. Replace the last else if (minStep >= 20 && timePassed >= 10) with simply else, as the condition would always be true if control reaches there.
2. Store the value 10 / (time * fps) in a private variable and use that directly, rather than evaluating it each time as the value remains constant.
You can also try replacing the if-else-if block with a switch-case block on a state variable in the class (with values as 0, 1, 2 signifying accelerating, constant speed and decelerating, you can define symbolic constants to hold the values). Initialize the variable with value 0 (accelerating), change it to 1 when minStep becomes 20 and so on. Point#2 still holds good in this case too.
-Dharmendra
Copy link to clipboard
Copied
thanks a lot : ) that gave me something to think about and for the rest of my code.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now