Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

How to optimize a acceleration / friction function more

New Here ,
Nov 29, 2013 Nov 29, 2013

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.

TOPICS
ActionScript
648
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Employee , Nov 29, 2013 Nov 29, 2013

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

...
Translate
Adobe Employee ,
Nov 29, 2013 Nov 29, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 30, 2013 Nov 30, 2013
LATEST

thanks a lot : ) that gave me something to think about and for the rest of my code.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines