0

I want to start a project. But, while doing a paperwork, i realized one situation as follows,

Say, we have 3 classes.

  1. Main.as,
  2. A.as,
  3. B.as

Here, "Main.as" is a central class which creates instance of A and B.

Class A has some function say "updatePosition(e:Event)" with ENTER_FRAME event.

Class B is required to get update from this "updatePosition(e:Event)" from class A via "Main.as"

How can this be achieved only with one ENTER_FRAME event and that of in A.as class only?

Rajneesh Gaikwad
  • 1,193
  • 2
  • 14
  • 30

2 Answers2

2

Here's one simple way to do it but I'm sure there are plenty of better ways (such as using custom events, or AS3 signals library):

Main.as

private function eFrame(e:Event) 
{
    a.runEvents();
    b.runEvents();
}

A.as. B.as

public function runEvents()
{
   // Run whatever events you need to run
}

This will give you the same effect as 3 ENTER_FRAME events but without the overhead, however you will have overhead from lots of function calls.

Pixel Elephant
  • 20,649
  • 9
  • 66
  • 83
0

Why don't you just put a single ENTER_FRAME handler in main.as and call the updates of the other classes from there?

public class Main extends Sprite
{
    private var _a:A = new A();
    private var _b:B = new B();
    public function Main()
    {
       if(stage) init();
       else addEventListener(Event.ADDED_TO_STAGE,init);

    }

    private function init(e:Event=null):void
    {
        removeEventListener(Event.ADDED_TO_STAGE,init);
        stage.addEventListener(Event.ENTER_FRAME,onEnterFrame);
    }

    private function onEnterFrame(e:Event):void
    {
          _a.updatePosition();
          _b.updatePosition();
    }

}
Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66