views:

55

answers:

2

Hi guys.. I am trying to pass custom event data from 1 class to another class...here is my codes..

a.as

private function onClick(event:MouseEvent):void{
   dispatchEvent(new YouTubeSearchEvent(YouTubeSearchEvent.CHANGE_VIDEO_READY, videoId));
    }

b.as

private var newVideoID:String;

public function get selectedVideoID():String{
        return newVideoID;
    } 


private function buildSingleCont(videoResult:Array):void{

    tn=new a();
    addChild(tn);
    tn.addEventListener(YouTubeSearchEvent.CHANGE_VIDEO_READY, getNewVideoID);

}

private function getNewVideoID(event:YouTubeSearchEvent):void{
        newVideoID=event.videoResult;

}

c.as

 // this is the main class.....
private var newvideoida:String;
public function c(){
 createSearchContainer()  //this method is called before b.as and c.as are called...
}    

private function createSearchContainer():void{
        searchContainer=new b();
        newvideoida=searchContainer.selectedVideoID; //I want to get b.as newVideoID variable
        trace(newvideoida);  //display nothing.....

        addChild(searchContainer);

        }

package com.search.events { import flash.events.Event;

public class YouTubeSearchEvent extends Event
{
    public static const FEED_VIDEO_READY:String="feed_video_ready";
    public static const CHANGE_VIDEO_READY:String="change_video_ready";

    public var videoResult:*;

    public function YouTubeSearchEvent(type:String, videoResult:*)
    {
        super(type);

        this.videoResult=videoResult;

    }
    public override function clone():Event { 
        return new YouTubeSearchEvent(this.type, this.videoResult); 
    }
}

}

I appreciate any help..

+2  A: 

Not seeing your actual custom event class makes it a bit hard to see where the error lies, but I guess you might have forgotten to override the clone() method:

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/events/Event.html#clone%28%29

"When creating your own custom Event class, you must override the inherited Event.clone() method in order for it to duplicate the properties of your custom class. If you do not set all the properties that you add in your event subclass, those properties will not have the correct values when listeners handle the redispatched event."

Quasimondo
I just updated my custom event in the post...I haven't slept for a whole night....my head is not with me now.... ...+1 though
Jerry
I also changed my c.as to make the codes more clear....
Jerry
So my guess was correct - no override clone() method in your custom event. You should give it a try. public override function clone():Event { return new YouTubeSearchEvent(this.type, this.videoResult); }
Quasimondo
I added your override method but still didn't have luck....my problem is that my c.as will never receive the event object from a.as....only b.as could receive it...weired....
Jerry
I got it now...all i need it bubble=true in my custom event...
Jerry
+1  A: 

You could also dispatch your event like this:

var event:YouTubeSearchEvent = new YouTubeSearchEvent(YouTubeSearchEvent.CHANGE_VIDEO_READY);
event.videoId = theOneVideoId;
dispatchEvent(event);

You need a listener to notice if the event is dispatched. So you need something like

searchContainer.addEventListener(YouTubeSearchEvent.CHANGE_VIDEO_READY,onChangeVideoReady);

If the event is now dispatched the method onChangeVideoReady(event:YouTubeSearchEvent) is called. There you have access to event.videoId

Maybe you have a look at the mate framework, where events are also very important.

hering