我正在尝试将自定义事件数据从1个类传递到另一个类......这是我的代码..
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);
}
包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);
}
}
}
我感谢任何帮助..
答案 0 :(得分:2)
没有看到你的实际自定义事件类使得查看错误位置有点困难,但我想你可能忘记覆盖clone()方法:
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/events/Event.html#clone%28%29
“在创建自己的自定义Event类时,必须覆盖继承的Event.clone()方法,以便它复制自定义类的属性。如果没有设置在事件中添加的所有属性子类,当侦听器处理redispatched事件时,这些属性将没有正确的值。“
答案 1 :(得分:1)
你也可以像这样派遣你的活动:
var event:YouTubeSearchEvent = new YouTubeSearchEvent(YouTubeSearchEvent.CHANGE_VIDEO_READY);
event.videoId = theOneVideoId;
dispatchEvent(event);
如果调度事件,您需要监听器注意。 所以你需要像
这样的东西searchContainer.addEventListener(YouTubeSearchEvent.CHANGE_VIDEO_READY,onChangeVideoReady);
如果现在调度该事件,则调用onChangeVideoReady(event:YouTubeSearchEvent)方法。 在那里你可以访问event.videoId
也许你看一下mate框架,其中事件也非常重要。