Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Preloader to estimate time for a function to finish?

Participant ,
Jan 24, 2013 Jan 24, 2013

Hello. I'm building an AIR project that uses images stored locally to perform some calculations using their bitmapData. In this app I browse my hard drive via an application menu command I created, select an image (using FileReference) and it loads. When it finishes loading, the calculations take place and the image is added to the stage. The larger the image, the longer this takes.

So far, so good.

I added a preloader to check the FileReference loading time and another for the loadBytes method I use later to be able to use the bitmapData. What surprised me was that both theses processes were very fast, but with larger images they were taking a long time to be added to the stage. Some tracing and commenting later, I discovered the culprit was one of the calculations I was doing, involving pixel counts. So my question is, is there any way to create a preloader for this function?

Thank you.

TOPICS
ActionScript
1.0K
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
Community Expert ,
Jan 24, 2013 Jan 24, 2013

there is but it's not as easy as normal preloading.

you can break your code into smaller chunks, each of which completes and allows a stage update.  here's a sample from a book being published later this year:

Loop types that execute from start to end before anything updates on stage include do-loops, for-loops and while loops. They cannot be used to animate objects because no matter what code you use to execute the loop and no matter what you do to (try and) slow it down, it will still execute from start to finish before anything changes on stage. They are appropriately used for rapid execution of code.

There is never a situation when you should intentionally slow these loops. However, there are situations when you might want to break these loops into more than one chunk so the stage can update between each chunk.

Chunks

For example, if you have a for-loop that takes 10 seconds to execute, your game will appear to freeze for 10 seconds after this loop starts. Nothing will update on stage and nothing will respond to user input. Either you should warn your user before starting that loop or you should break that loop into several smaller chunks that allow visual updates to the user so they do not think your game is broken.

For example, this for-loop that adds odd numbers (and shows the first m odd numbers sum to m*m) freezes my Flash Player for about 9 seconds.

var i:Number;

var n:Number=3000000000;

var s:Number=0;

var startI:Number=1;

var endI:Number=n

var startTime:int=getTimer();

for (i=startI; i<endI; i+=2) {

       s+=i;

}

// 9 seconds

trace((getTimer()-startTime)/1000,s,n*n/4,s-n*n/4);

The following technique shows how to break this (and any other for-loop) into chunks that allow the Flash Player to update every second.

var i:Number;

var n:Number=3000000000;

var s:Number=0;

var startTime:int=getTimer();

// This is the number chunks into which the previous for-loop will broken. If the // previous for-loop took about 9 seconds, using 10 chunks means there will be updates // about every 0.9 seconds.

var chunks:int=10;

var startI:Number=1;

var endI:Number=n/chunks;

var t:Timer=new Timer(100,1);

t.addEventListener(TimerEvent.TIMER,f);

f();

function f(e:Event=null):void {

       for (i=startI; i<endI; i+=2) {

              s+=i;

       }

       trace("stage update",startI,endI,s);

       if (endI<n) {

              t.reset();

              t.start();

       } else {

              trace((getTimer()-startTime)/1000,s,n*n/4,s-n*n/4);

       }

       startI+=n/chunks;

       endI+=n/chunks;

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
Participant ,
Jan 24, 2013 Jan 24, 2013

@kglad thank you very much. I need to take a long look at this to make sure I understand everything before adapting it to my code, but I think it might solve my problem.

In this:

  1. t.addEventListener(TimerEvent.TIMER,f);

I don't understand the "a." part...

And if I understand correctly, f() is called every 0,1 seconds, but since it takes about 0,9 seconds to finish, each chunk is only processed after the previous one finishes, it that it?

Also, if I were to use something other than Timer - the only thing that occurs is yet another for loop - to divide the chunks, do you think it would work or would it add to the problem?

Thanks.

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
Community Expert ,
Jan 24, 2013 Jan 24, 2013

that a. is an artifact from copying ms word text which i removed.

the for-loop is broken into several different chunks.  each chunk executes from start to end. 

because nothing can update on stage (so you can give no user feedback) while a for-loop executes, the idea is to break a long executing for-loop into several smaller chunks so you can update the display/stage and give user feedback.

the timer is designed to execute as quickly as possible.

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
Participant ,
Jan 25, 2013 Jan 25, 2013

Again, thank you. I had no idea that a for-loop would prevent screen rendering.

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
Community Expert ,
Jan 25, 2013 Jan 25, 2013
LATEST

you're welcome.

Loop types that execute from start to end before anything updates on stage include do-loops, for-loops and while loops. They cannot be used to animate objects because no matter what code you use to execute the loop and no matter what you do to (try and) slow it down, it will still execute from start to finish before anything changes on stage. They are appropriately used for rapid execution of 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
LEGEND ,
Jan 24, 2013 Jan 24, 2013

I like this tutorial it has helped me in the past: http://www.senocular.com/flash/tutorials/asyncoperations/

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
Participant ,
Jan 24, 2013 Jan 24, 2013

@Rothrock, thank you, I'll dig into it.

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