A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: [RESOLVED] Dispatch and Listen for a Global Event

Hybrid View

  1. #1
    Senior Member
    Join Date
    Jan 2006
    Posts
    133
    Quote Originally Posted by 5TonsOfFlax View Post
    Yes, you're right. I apologize for not trying to compile it before posting.
    Meh, no problem, that's what the Compiler Panel is for! The tricky part is when nothing works and you DON'T get errors =)

    I was just posting for completeness for future viewers.

  2. #2
    Senior Member
    Join Date
    Jan 2006
    Posts
    133
    Oops... not done yet apparently...

    Now I'm encountering an interesting problem when I try to remove the listeners.

    In ClassA (the dispatcher) I have:
    Actionscript Code:
    Broadcaster.broadcastEvent("someEvent");

    This line within ClassA is associated with an interval, so it basically tells the Broadcaster class to broadcast a "someEvent" event once a second.

    In ClassB (the listening class) I have:
    Actionscript Code:
    // in ClassB constructor
    Broadcaster.addBroadcastListener("someEvent", onSomeEvent, false, 0, true);

    // ClassB methods
    private function onSomeEvent(e:Event):void{
        trace("Broadcast Received");
    }

    This all works great. I get "Broadcast Received" traced to output panel about once a second.
    However, now I'm concerned with what happens when I want to destroy a ClassB instance and (hopefully) flag it for garbage collection. So, each instance of ClassB has a kill() method (called when it is removed from stage via removeChild) designed to remove "standard" listeners, children and other sub-processes to make sure that there aren't any extraneous references.
    Now, I have removed the ClassB instance from the Display List, and set all external/parent references to the ClassB instance to "null"; however, I'm still getting "Broadcast Received" traced to the output panel... even though ClassB instance that has the trace() script in it has been "destroyed".
    I thought that would easily be solved by removing the broadcast listener:
    Actionscript Code:
    public function kill():void{
        Broadcaster.removeEventListener("someEvent", onSomeEvent, false);
    }

    I got a strange compile-time error:
    Code:
    1061: Call to a possibly undefined method removeEventListener through a reference with static type Class.
    This threw me for a loop because Broadcaster extends EventDispatcher, so shouldn't it inherit the removeEventListener() method? Is there some reason I would need to override the EventDispatcher method?

    Or, do I need to create a special removeBroadcastListener() method in Broadcaster in order to remove any listeners added? If so, what would that look like??

    Thanks!

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