2010-08-09 52 views
0

我想从1级传递自定义事件数据到其他类...这是我的代码..AS3传递自定义事件数据询问

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); 
    } 
} 

}

我感谢所有帮助..

+1

如果你发布完整的课程定义,它会更容易帮助你... – OXMO456 2010-08-09 10:04:26

+0

我现在得到了....我需要的是bubble = true在我的自定义事件中... – FlyingCat 2010-08-09 19:25:12

回答

2

没有看到实际的自定义事件类使它有点难以看到那里的错误所在,但我猜你可能已经忘记了重写的clone()方法:

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

“在创建自己的自定义事件类,则必须覆盖继承的事件.clone()方法,以便它复制自定义类的属性。如果您没有设置所有你在事件子类中添加的属性,当侦听器处理重新调度的事件时,这些属性将不会有正确的价值观“

+0

我刚更新了自定义事件在帖子中......我没有睡一整夜......我的脑袋现在不在我身边...... + 1虽然 – FlyingCat 2010-08-09 10:51:50

+0

我也改变了我的c.as以使代码更清晰.... – FlyingCat 2010-08-09 10:59:32

+0

所以我的猜测是正确的 - 在你的自定义事件中没有覆盖clone()方法。你应该试试看。 public override function clone():Event { return new YouTubeSearchEvent(this.type,this.videoResult); } – Quasimondo 2010-08-09 16:30:43

1

你也可以派你的事件是这样的:

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

你需要一个听众注意,如果事件被分派 所以,你需要像

searchContainer.addEventListener(YouTubeSearchEvent.CHANGE_VIDEO_READY,onChangeVideoReady); 

如果该事件现已分派方法onChangeVideoReady。(事件:YouTubeSearchEvent)被称为 在那里你可以访问event.videoId

也许你看看mate框架,其中事件也非常重要。

相关问题