PDA

View Full Version : Feedback during a long loop


somethingkindawierd
11-16-2005, 07:52 PM
Hello,
My question is this: How do I show the progress of a really long for(i=0; i<variable; i++) loop by animating a progress bar component or some custom MovieClip? I do not need detailed progress, just motion to show that the browser has not locked up. I am using Flash MX 2004, the movie published with ActionScript 2.0 and Flash 7 compatibility.

Details of my project and why I have not yet succeeded: I have a flash movie that performs some drawing functions using lineTo() - I have a loop that reads in the values from an XML file, loops through them and draws - sometimes this can take a long time so I would like a progress bar to animate while it happens. My problem is that when I enter this loop no other animations are performed until it is done...I tried forcing a progress bar component to update using invalidate() and redraw(), I tried my own animated tween, I even tried an animated gif as a layer on top of the Flash movie in my browser - all of the animations stop when the "for" loop is entered, and start back up when it completes. I also noticed that the "onEnterFrame" function is not called while the loop is working. So, I think I am missunderstanding something about Flash - how is a loop watched for feedback? Or, how can I reprogram work like this so that I can watch it, because I need the feedback.

Thanks in advance!
jON bEEBE
BIG Images

arkanoid2k
11-18-2005, 02:19 AM
what are you using to load the XML?? loadVars() or loadVariables ??

somethingkindawierd
11-30-2005, 01:13 AM
Neither. I am using the XML.load() command. My code looks like this.

var xmlFile:XML = new XML();
xmlFile.load(theFilePath+theXMLFile);

xmlFile.onLoad = function(bSuccess:Boolean):Void {
if (bSuccess) {
makeDocument(xmlFile);
}
}

The function makeDocument(xmlFile) is the one taking FOREVER and not allowing me feedback while it loops through the xml nodes doing its work. I just get the xml children and store them in an array and it does this:

for (a=(array.length-1); a>=0; a--) { // do the work here }

And while it is in this loop nothing else happens.

jON bEEBE

oldnewbie
11-30-2005, 01:57 AM
Stick in some traces (if testing in authoring...) or some messages through a temporary textfield on stage.

Rossman
11-30-2005, 09:09 PM
Perhaps calling updateAfterEvent() in that tight loop would fix your problem? Not sure though, I've never used it before...

somethingkindawierd
12-01-2005, 08:12 AM
First, many thanks to everyone for your thoughts...

Here are the results of my endeavors...
The trace commands did not do the trick...traces were executed but the graphics were not redrawn.
updateAfterEvent() did nothing as well.

I finally found a detailed explanation here:
http://www.macromedia.com/cfusion/webforums/forum/messageview.cfm?catid=288&threadid=1045183&highlight_key=y&keyword1=loop

By setting an interval, and incrementing my count variable in the interval function I was able to use the interval exactly like my for (a=(array.length-1); a>=0; a--) { // do the work here } loop. If i did not use an updateAfterEvent() command within the interval function the movie refreshed at the frame rate, otherwise the updateAfterEvent() did cause the movie to refresh more often if the interval was able to run more than once within a single frame of the root movie. So, if the interval function ran 2 times per frame, at 12 frames per second, the updateAfterEvent() caused 2*12 = 24 refreshes.

It worked like this (by the way...I am purposefully looping backwards from the array count to zero):

var i:Number = arrayOfItemsToProcess.length
_nInterval = setInterval(this, "processItems", 1, arrayOfItemsToProcess);

function processItems(theArray:Array):Void {
i = i-1;
// CHECK TO SEE IF WE ARE AT THE END OF OUR ARRAY
// IF WE ARE THEN CLEAR THE INTERVAL
if (i<0) { clearInterval(_nInterval) };

// IF WE ARRIVED HERE WE HAVE MORE OBJECTS TO PROCESS
var theItem:Object = theArray[i];
// do work on the item here...
}

Hope this helps someone.
jON bEEBe
BIG Images
www.big-images.com
www.beebeography.com

Rossman
12-01-2005, 04:41 PM
I knew this way would work, but was hoping there was a simpler answer.

Glad you got it sorted, thanks for coming back and sharing your discovery!