?
Solved

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

Posted on 2006-05-04
16
Medium Priority
?
277 Views
Last Modified: 2008-03-03
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?
0
Comment
Question by:acekz
16 Comments
 
LVL 5

Expert Comment

by:wings_gaurav
ID: 16607607
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  
0
 
LVL 37

Assisted Solution

by:CyanBlue
CyanBlue earned 400 total points
ID: 16608005
Howdy...

I'd probably do this...
I'd position the text elements as much as I can possibly position on to the stage...  and whenever I know the image is loaded, I'd call a function that will rearrange the whole thing on the stage which will properly position the items on the stage since you then know the width/height of the loaded image...

That's my 2 cents on that...  ;)

CyanBlue
0
 

Author Comment

by:acekz
ID: 16610345
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Expert Comment

by:jack_p50
ID: 16612942
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
0
 

Author Comment

by:acekz
ID: 16614257
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...
0
 
LVL 4

Expert Comment

by:jack_p50
ID: 16614369
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...
0
 

Author Comment

by:acekz
ID: 16614567
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?
0
 
LVL 4

Expert Comment

by:jack_p50
ID: 16614806
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).
0
 
LVL 4

Expert Comment

by:jack_p50
ID: 16614829
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
0
 

Author Comment

by:acekz
ID: 16615005
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?
0
 
LVL 4

Accepted Solution

by:
jack_p50 earned 1600 total points
ID: 16615104
1) I'd do it as a class, yeah.
2) Talking about overhead, your current way may be somewhat more hairy, but I'm sure that it works faster than it would were you to rewrite it another way, because loading several images in parallel (what you have now) works faster than loading them sequentially (what you'll have if you rewrite it). If you absolutely need cleaner code and load times are not so critical, then go ahead and rewrite it, but maybe you can just clean up the current code (i.e. structure it better, use classes) without heavy mods. My 2 cents
0
 

Author Comment

by:acekz
ID: 16615130
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!!!
0
 
LVL 4

Expert Comment

by:jack_p50
ID: 16615184
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.
0
 

Author Comment

by:acekz
ID: 16615204
sweet, thanks! that's what I am gonna do...
0
 
LVL 4

Expert Comment

by:jack_p50
ID: 16615217
good luck, dude
0
 

Author Comment

by:acekz
ID: 16615232
Hey, i am about to post another question...please watch for it, ok...it's another movie loading thing...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question