7 Replies Latest reply on Apr 28, 2008 2:44 PM by Benji2008

    Make code wait, till other code is ready

    Benji2008 Level 1
      Hey there,

      sorry for the silly topic title, I'm not sure how to phrase my problem :$

      I have an mxml page.. which initializes a manager in which all the background stuff happens (xml gets loaded etc etc),
      a bit further in the mxml page (still in de CODE part of course), I'd like to collect an array that is available in the manager.. however, after testing out various scenarious, i've come to the conclusion that it looks like (i'm not sure of course), that the call I do in the mxml page happens FIRST & AFTER the manager does it's work..

      Is there any way to put certain code "on hold", until i'm ready to execute it? Could I somehow use a listener for this problem?

      private function init():void{
      manager = new XMLLoader();


      I was thinking I could use some Eventlistener- Loader ?

      .addEventListener("ready",fillArray,false,0,true); = in init();

      actual function = fillArray(evt:Event)

      -- not sure how do fix that though..

      So, as u can see above, i first initialize the manager! (so i'd expect it to fill the class with the necessary data).
      After I call fillArray which calls certain functions to display the data from the manager class. (however when i try to display the data it gives NULL back (as it's still empty).. If i trace the data IN the manager, it IS displayed.. making me believe the manger does it's work after the fillArray()..

      Sorry if this sounds gibberish, thanks to any of you who read this to try to help :)
        • 1. Re: Make code wait, till other code is ready
          jpsoul Level 1
          huh? which class is fillArray() in? what is it doing?
          • 2. Re: Make code wait, till other code is ready
            ntsiii Level 3
            There is no code blocking in Flex, so events are the way to handle what you need.

            Also, all data service calls (HTTPService,WebService, RemoteObject) are asynchronous. You MUST use events to handle the result. You can NOT access the result in the same function in which you invoke send()

            Are you loading data before you run your initialze code? If so, declare a result handler function as a listener for the result event of the data call. In that function, launch your fillArray code.

            • 3. Make code wait, till other code is ready
              Benji2008 Level 1
              @jpsoul: fillArray is a function which "should" collect the data from the managerclass (which contains the data from my xml files).

              @ntsii - i load my data with a call to the manager, which gives instructions to grab all the data from the xml files! so yes, i guess i do load the data before initializing the code..

              can you plz point me in the direction of some help on how to use result-handlers ?
              or show an example code of how i can define that i have to wait for the other class to finish first?

              thanks for your time ppl :) I really appreciate it!
              • 4. Make code wait, till other code is ready
                Benji2008 Level 1
                Alright- UPDATE!

                Thanks for your help :) I added an eventlistener (the mouse click one), for the moment.. Now my program works :)

                Just one detail LOL - I don't need mouseclick, I need an event, which will make sure the function i call in the event, is launched WHEN the other function is done.. I searched through the functions, but dont seem to find the right one, any help is welcome :$

                private function init():void{
                cm = new CoverXMLLoader();

                So I need something else instead of mouseEvent, but dont know which one :$
                • 5. Re: Make code wait, till other code is ready
                  jylaxx Level 1
                  Spend some time to read the documentation about events and be sure to understand it !!!

                  In your case you have to set a listener to an event of the loader (cm) !
                  Find in the doc which type of event is dispatched when the load is complete and add a listener on it.

                  • 6. Make code wait, till other code is ready
                    Benji2008 Level 1
                    @jylaxx , thanks!!!

                    I've been reading in the docs, it's quite straightforward how to add actionlisteners on buttons etc.. (when you expect userInteraction).. but like you said, I have to add an EventListener to the loader class, but I don't know what to "hang" it on if you know what I mean.. I can't figure that out with the docs..

                    atm.. in the loader class, i put:

                    stage.addEventListener(Event.ENTER_FRAME,main.fillArray); (on the place where i want the function to be called),
                    but i get: TypeError: Error #1009: Cannot access a property or method of a null object reference.

                    Actually, I think I'm not sure how to call the function from the main class :/ (all the examples I found call the function in the same class as the listener is in )
                    • 7. Re: Make code wait, till other code is ready
                      Benji2008 Level 1
                      ok - I figured it out. :p I'm gonna reply here, in case someone once comes across the same question as I have ..

                      thanks to everyone who replied, you all helped me very well! I found EXACTLY what i needed on this page:
                      The example is great!

                      http://livedocs.adobe.com/flex/2/langref/flash/events/EventDispatcher.html#includeExamples Summary