Executing AS3 Function Asynchronously

I have a bit of trouble understanding some of the tutorials for this online, so I ask here. (Using ActionScript 3, Adobe AIR, and Flash Professional CS5.5)

I have a very heavy function in my AS3 document class that I need to run asynchronously, so it does not stop the MovieClip code itself (don't ask me why this should be so).

So just enter, how do I run this document class function (StartNow) asynchronously? The code can be placed in a document class or in a movie clip, I don't care where. This seems to be a relatively simple and common practice, but all my research does not dig anything out.

Thanks!

+4
source share
2 answers

There is no such function in Flash as executing a function asynchronously, you have to do it yourself if you do not want to use Workers (for example, Vesper). Workers give you a separate process. Otherwise, you will have to break your calculation into pieces. Here's how you do it:

Tracing images is a very difficult operation. This is not so, but just for illustration. This simple for-loop runs on a frame and causes a lower frame rate, since they are all calculated before the frame is actually displayed.

for(var i:int = 0; i < 1000; i ++) { trace(i); // heavy calculation here } 

So, you need to break the calculation into pieces and break it in order to be able to run the calculation over time.

To do this, you need to create a function that simply takes part of the loop every time:

 calculatePart(0, 1000, 20); function calculatePart(startIndex:int, endIndex:int, amountPerRun:int) { for(var i:int = startIndex; (i < startIndex + amountPerRun) || (i < endIndex); i ++) { trace(i); // heavy calculation here } if (i < endIndex) { calculatePart(i, endIndex, amountPerRun); } } 

This is actually the same function as the simple for loop in the first code, and it also prints 1000 traces. He is ready to work in parts, but this is not async yet. Now we can easily change the function, so the function works over time. For this, I use setTimeout . You can also use the ENTER_FRAME event-listener or Timer class for this, but for this example I will try to clear it.

 calculatePart(0, 1000, 20, 100); function calculatePart(startIndex:int, endIndex:int, amountPerRun:int, timeBeforeNextRun:Number) { for(var i:int = startIndex; (i < startIndex + amountPerRun) && (i < endIndex); i ++) { trace(i); // heavy calculation here } if (i < endIndex) { setTimeout(calculatePart, timeBeforeNextRun, i, endIndex, amountPerRun, timeBeforeNextRun); } } 

As you can see, I added the timeBeforeNextRun parameter. If you run this example, you will see that it takes 100 milliseconds to output 20 traces.

If you set it very low, the calculation will be delivered very quickly, but you won’t be able to get extra speed just trying to do more in less time. You have to play with time and quantity variables, you can check which one actually gives the best performance (or less lag).

  // more time between a run, less calculations per run calculatePart(0, 1000, 30, 10); // more calculations per run, more time between a run calculatePart(0, 1000, 100, 30); 

Hope this helps.

If you want to use a more reasonable time calculation, I found this utility class very useful, which measures how much time the calculation actually took and changed the time.

+2
source

If your goal is Flash Player 11.4, there are Worker objects that can be assigned such a heavy function. I did not have FP11, and in the end I created a procedural generator that lasted more than 300 seconds per iteration. I had to use a state-based approach in conjunction with an input frame receiver. In my cee, the whole process of creating a complex process was divided into logical pieces that were small enough to be completed in a reasonable amount of time, and had variable tracking of the current generation phase. Thus, when another frame is called a generation function, it reads the last completed step from this variable, performs one additional step with its data set, saves the new value and goes beyond the frame. This, as it is, is not a pure asynchronous process, but a pseudo-multitasking approach, it may suit you if your function, which forces your SWF gap to be shared.

+4
source

Source: https://habr.com/ru/post/1447670/


All Articles