adam
adam

Reputation: 1

trouble getting Actionsript3 custom event to work between flex and flash

I'm trying to make objects made in flash to dispatch events out to my flex app.

Here's the scenario:

  1. I made my custom event:

    package classes
    {
            import flash.display.MovieClip;
            import flash.events.Event;
    
          public class DecisionNodeEvent extends Event
     {   
    
    public static const NEW_DECISION_NODE:String = 'new_decision_node';
    public var node:MovieClip;
    
    public function DecisionNodeEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false, node:MovieClip =null)
    {
        super(type, bubbles, cancelable);
        this.node = node;
    
    }
    
    override public function clone():Event
    {
        return new DecisionNodeEvent(type, bubbles, cancelable, node);
    }
    
  2. I then dispatch it from my custom object (a Movieclip):

    var event:DecisionNodeEvent = new DecisionNodeEvent(DecisionNodeEvent.NEW_DECISION_NODE);
    dispatchEvent(event);
    
  3. and finally, I create an instance of the flash object in Flex and set up a listener and handler for it.

        nodeZero = new Node(0,null);
        nodeZero.addEventListener(DecisionNodeEvent.NEW_DECISION_NODE, decisionNodeAdded);
    
         .......
    
        private function decisionNodeAdded(event:DecisionNodeEvent):void
        {
    
                trace('the event came to the main Flex app');
        }
    

I have tested to make sure that the events gets dispatched. It does, and I can see the traces come up on the Flex console. It just doesn't seem to make it to the event handler. this is extremely frustrating :( Can anybody out there help me out please??

Upvotes: 0

Views: 95

Answers (3)

Amy Blankenship
Amy Blankenship

Reputation: 11

If the event is dispatched in the constructor (the code that runs when you use the new keyword), it has already happened by the time you get to the next line and add the event listener.

HTH;

Amy

Upvotes: 1

adam
adam

Reputation: 1

Not passing the node was not the issue I was having (I took out the node pass for debugging purposes). The issue was that no event was being sent over to where the listener was, thus the handler was not getting fired. thanks for the assistance though.

I fixed the problem by knocking the dispatch event out of the constructor. I think the listener wasn't being created until well after the dispatcher broadcasted the event (just a theory) either way it's working now so thank you all :)

Upvotes: 0

Nate
Nate

Reputation: 2936

You're not passing the node when you dispatch the event (which is the whole point of the custom event, right?), I think it's supposed to look more like this :

 this.dispatchEvent( new DecisionNodeEvent(DecisionNodeEvent.NEW_DECISION_NODE,false,false,this) );

=)

Upvotes: 1

Related Questions