dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Events firing before listener is set.

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    18

    Events firing before listener is set.

    Hi All,

    I keep running into problems where I want to add an event listener onto a class instance RIGHT after it's instantiated, like this:

    Code:
     . . . 
    storiesXML = new XML(XMLdata);
    storyBook = new vStories(XML(storiesXML.stories));
    storyBook.addEventListener("storyBookLoaded", storyBookLoaded);
    . . .
    but by the time we arrive at the third line, the "storyBookLoaded" event has very often (inconsistent) already been dispatched from my class instance:

    Code:
    var evt:Event = new Event("storyBookLoaded", true, false);
    dispatchEvent( evt );
    ... (even though quite a lot has happened in the class before that.) To be clear, I don't want to add storyBook as a display child, and even if I did, the addChild() method would have to be called after instantiation of storyBook, so we'd have the same problem.

    I know I could set a timer in the vStories class to artificially delay the dispatching of the event, but that seems really hack-ish, and would have a real compounding problem if I have to do it too often as everything builds up.

    Anyone have any better practices for me to make sure custom communication events happen after their listeners are set?

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    It shouldn't matter - the events fired in a given frame aren't handled until the beginning of the following frame so you should have until at least the end of your function to attach listeners.
    Please use [php] or [code] tags, and mark your threads resolved 8)

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    18
    I'm not using any timeline-based development... all in code.

  4. #4
    Senior Member
    Join Date
    May 2004
    Posts
    226
    I assume the vStories class is dispatching the event from its constructor? There is no way to add a listener to an object that hasn't been created yet and if the object is dispatching an event at instantiation no one will be able to receive it. There are some ways around this but generally they defeat the loose-coupling benefits of events. What would probably be easiest is to add a setXML function to vStories so the dispatch can be deferfed until after the listener has been added:
    .
    PHP Code:
    storiesXML = new XML(XMLdata);
    storyBook = new vStories();
    storyBook.addEventListener("storyBookLoaded"storyBookLoaded);
    storyBook.setXML(XML(storiesXML.stories)); 
    Then in the vStoreis class add something like this:
    PHP Code:
    public function setXMLxml:XML ):void {
        
    //process the xml, then dispatch the event
        
    var evt:Event = new Event("storyBookLoaded"truefalse);
        
    dispatchEventevt );


  5. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    18
    Thanks v5000.

    That's a great suggestion. I'm a little surprised that there's no way to just reliably set an external listener on an object pre-execution, but this workaround should work.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center