Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3: Run methods in an ordered sequence

I have a class with several methods I must run in an specific order. So, I created a kind of chain where each one calls the next in its last sentence (sometimes with calls, sometimes dispatching events):

internal function a(evt:SwapEvent):void {   //started from custom event
  ...
  b();
}

private function b():void {
  ...
  bTimer = new Timer(bTime*1000,1);
  bTimer.addEventListener(TimerEvent.TIMER, bEnd);
  bTimer.start();
}

private function bEnd(evt:TimerEvent):void {
  bTimer.removeEventListener(TimerEvent.TIMER, bEnd);
  bTimer = null;                    
  c();
}

private function c():void {
  ...
  dispatchEvent(new CustomEvents(CustomEvents.NEXT_FRAME,1));
}
....
private function g():void {
  // tell the second class the sequence finished
}

The problem is at some point and before arriving at last method, I need to run again a sub-sequence, let's say from function c() to e(). And it's causing problems in the form of an increasing delay between functions (I have 2 timers)

I guess the solution is something like this:

a();
b();
...
if (some condition) {
  c();
  ...
  e();
}
...
f();
g();

But, as far as I know, Flash Player doesn't make a synchronous execution. i.e., it doesn't wait until method a() completes to execute b()

Any idea/suggestion on a clean and bullet-proof implementation? This application will run in an endless loop 24x7

Thanks in advance,

like image 745
hsands Avatar asked Feb 26 '23 00:02

hsands


1 Answers

After reading your code properly, yes, you are right. b() gets executed as that line of code is reached.

I might be tempted to create a queue of methods to execute. Execute one, check to see if you have time to execute another before the frame needs updating, and repeat. You can always insert new commands in to the queue at any time, so b() could insert endB() next in the queue.

Both the sequencers at http://www.dpdk.nl/opensource/running-tasks-in-order-with-a-task-based-sequence-manager and http://as3.casalib.org/docs/org_casalib_time_Sequence.html should do what you need. The former might be a bit of overkill as it looks like you need to create individual classes for each of your tasks which may be a little too much overhead. On the other hand the Conditional Tasks look like they are exactly what you need. The latter being simpler in that you can specify methods to execute. However, there doesn't seem to be a built in way to condition the tasks, but that may just be as easy as creating a task that conditionally adds tasks.

It might help to know a bit more how the conditions work.

Other points:

Tail calls are not very efficient in AS. Think of it as adding more to the stack every time you call a method. Once the method returns (even if you don't explicitly use return) the method gets knocked off the stack and the previously called method will continue to execute. So the more tail calls, the bigger the stack and the more to clean up.

As soon you start executing code the player hangs until the code has completely run and execution has returned to the player. You have around a 15 second execution limit before the player will kill your script, so you have to account for that when endlessly executing code. The solution is to execute some code then wait till the next frame to execute more.

You don't have to recreate your Timers, you can create them once as instance variables and just call start. The timers will return execution to the player (if no more method calls are made).

A somewhat simplified version, but I'm sure you get the picture.

ADDENDUM: I think you should look in to the Command Pattern (http://en.wikipedia.org/wiki/Command_pattern). You can store an array of commands to be executed (either synchronously, or asynchronously) and when one returns or dispatches a complete event you execute the next. I think you can create a simple implementation of the Command Pattern to do what you need without all the overhead of sequencers I mentioned earlier, but it's always good to get an idea of how others have done it.

like image 113
Joony Avatar answered Mar 07 '23 02:03

Joony