AS3 EnterFrame EventListener

Listen

EventListener EVENT.ENTER_FRAME as we all know is useful to trigger function you want to check up on conditions for a indefinite period of time. e.g. A check for game over state in a flash game. However, to control the flow of the your flash projects, usually we build only one of these and employ it to the stage trigger various function to have a better control over the code.

However, I noticed that ENTER FRAME does have some setbacks. I list them accordingly below:-

  1. Cannot be used as a consistent timer intervals.
    ENTER FRAME is not very good at managing actual timer intevals, to do that best use timer interval script which fire every actual seconds. Try not to use Timer Interval or any Tween Scripts inside ENTER FRAME, it will screw things up.
  2. If you nest an ENTER FRAME function within a function the variables are not passed in. Use a global variable. e.g. see like the code below
  3. Is a good practice to set a boolean on top of the first condition checker of the ENTER FRAME function, it helps when you are doing some hitTest and you only want it to return one value after the first Hit. See second code below.
  4. Try to Remove Listeners once you do not need them anymore. e.g. If you are developing a game and it’s game over etc.
	function MySuperEnterFrameFunc(myvalue){
 
	   stage.addEventListener(Event.ENTER_FRAME,fireRoutineFunction);
	   function fireRoutineFunction(evt:Event){
                trace("hello"+myvalue);
	   }

Explanation : Scenario Script 1

I’ve noticed you cannot pass in a internal variable inside a enterframe function within a function. You will most probably get values but, are highly volatile because of the EnterFrame nature, use a more global variable which is declared once.

	function MySuperEnterFrameFunc(myvalue){
           if(isOK){
	      stage.addEventListener(Event.ENTER_FRAME,fireRoutineFunction);
	      function fireRoutineFunction(evt:Event){
                   trace("hello"+myvalue);
	      }
            }

Explanation : Scenario Script 2

Adding a isOK boolean trigger which is a global variable help save up alot of CPU time and have your codes slightly more optimize. It basically does fire a EnterFrame function when there’s no proper boolean value.