Link to home
Start Free TrialLog in
Avatar of acekz
acekzFlag for United States of America

asked on

"pause" a for loop or a while loop until current action is completed? (Evant Dispatch?)

Ok, I read this article on Macromedia, err Adobe...it is a great article on the use of an Event Dispatcher to assign listeners for your custom events instead of running some sort of loop or interval to check over and over...Great concept and I know it works...

My quandry is using this methodology inside a loop (for or while, etc.).

Here's the deal. I am loading data into my Flash movie from XML. I load the XML file and loop through the nodes of the XML file and display my content as I loop through the XML object. Make sense?

the issue is that some of my content tells flash to load images or .swf files (using a loader class I have created/extended) as i go through. You may start to see my issue here...

I need to position other display elements based on the size and location of these loaded clips, but you can't get that data until it is completely loaded. In my class now I have an onLoadInit function that sets a value inside the class instance to true to say it's loaded. each time I load an image with this class, I immediately call create a unique setinterval id for each image that calls a function until the "loaded" value is true for ALL clips loaded then it executes another function to loop through all the elements and repostion them accordingly. This works great and fast right now, but I am not loading a lot of elements YET.

So back to the original story...it would be great to create event dispatch events and listeners for each element I am loading and then execute what I need to after that...

The ideal way would be to load the image, pause the loop until the listener receives the event dispatch event and then the rest of the loop executes thus allowing each element on stage to be positioned in order and from one loop through all the data instead of multiple loops.

So, can I do this? Can I "pause" the loop through the XML object to wait for each item to load so that all elements have thier properties before continuing to load the next and position everything the first time through?
Avatar of wings_gaurav
wings_gaurav

no pausing code segments (sleep, wait ) is not possible in flash. However, it very much possible to do this with a slightly different design approach. Let's say when we are parsing the xml and we have to load an swf/jpeg whole path we have encountered in the xml. at this point we store the reference of the current node instantiate a moviecliploader load the file and return from the function. the moviecliploader will notify/raise vent (call a function) upon this even we resume from the stored reference. although this is not going to be simple but is workable.

-wings  
SOLUTION
Avatar of CyanBlue
CyanBlue
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of acekz

ASKER

wings...thanks for the confirmation about pausing the code...I see what you are saying, but it seems pretty heavy...thought about that one...

CyanBlue - this is what I am currently doing now...I just drop everything on stage set _visible = false, then I go through it again once I know all the images are loaded and then reposition them all and set _visible = true...

Anybody got anything more?
if only flash was multi-thread... there's nothing flash can do here.
you can put your code in, for example, onEnterFrame or set up an interval...
the code should check if the "waiting for event" flag is set - if yes, do nothing, if no,
continue parsing the xml. the current parsing state should be saved of course.
using onEnterFrame/interval kinda defeats the EventDispatcher advantage somewhat
Avatar of acekz

ASKER

Ok...I think i get what you are saying...
So I loop through my XML object...when I get to a node that loads an image or movie, I save the parsing state and wait for my event dispatcher to fire and then "resume" the parsing state...

How would I resume that if I am looping through the XML and I exit the loop ( i guess with a break statement) wait for my event then try to "resume" the loop where I left off?...

I guess I could save the node name and count of my different loops, but that could get hairy...
Somethink like that. I'd save the reference to the last parsed node. When you resume, your code should start from nextSibling of this node. You probably don't need a counter, IMAO...
Avatar of acekz

ASKER

Alright, next question is the way it works is that I have my parent node that has child nodes which have child nodes that contain the elements I am loading...right now I am naming my objects based on a set prefix like "node" followed by the count value for i an j in my for loops...make sense...when I return to my XML parsing i not only need to know what's next, but what the names of my previous elements were for postioning/sizing purposes...i guess i could use the node name or does the XML object have a unique id for every node it contains that I could use? Am I making any sense?
you can maintain a counter (defined outside your parsing function) or add everything that's already parsed (objects, IDs, whatever) to an array (defined outside your parsing function).
you need to think if all that complexity (not that it's VERY complex, but complex nevertheless) is really necessary - maybe it's possible to do it in two stages... 1) parse and load everything, dont' position anything; then 2) parse again / use objects stored in arrays and position everything
Avatar of acekz

ASKER

That's pretty much what I am doing now...

I parse the XML, then I use a method to check if all the images are loaded, once they are all loaded, I go through once more and postion the elements...I do need to clean it up, I was just wondering if I could do it all on one pass without as much overhead...

Do you think that I could write a method/class that takes an XML object that i give it, with certain necessary parameters, and go through each node, not in a loop, but it calls another function (or itself) when all objects of a node are finished until there are no more nodes?

As I am thinking this might be more crazy, but it might work...i would be creating my own loop i guess...it might be heavier...what do you think?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of acekz

ASKER

I like your 2 cents...I am using classes now, but it could be cleaner...thanks a ton for your time on this! If you think of anything else, please post!!!
I don't think I can add anything to what I've said already. Flash doesn't have any facilities to ease concurrent programming / async IO, so writing something like that will inevitably get somewhat hairy. It's better to rewrite your current code from scratch in the same vein as it is already than to rewrite it in the way you mentioned in the question.
Avatar of acekz

ASKER

sweet, thanks! that's what I am gonna do...
good luck, dude
Avatar of acekz

ASKER

Hey, i am about to post another question...please watch for it, ok...it's another movie loading thing...