代码之家  ›  专栏  ›  技术社区  ›  FlyingCat

AS3传递自定义事件数据问题

  •  0
  • FlyingCat  · 技术社区  · 15 年前

    我正在尝试将自定义事件数据从一个类传递到另一个类…这是我的代码。。

    a、 作为

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

    b、 作为

    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、 作为

     // 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 {

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

    }

    2 回复  |  直到 15 年前
        1
  •  2
  •   Quasimondo    15 年前

    如果看不到实际的自定义事件类,则很难看到错误所在,但我猜您可能忘记重写clone()方法:

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

    创建自己的自定义事件类时,必须重写继承的Event.clone()方法,以便它复制自定义类的属性。如果未设置添加到事件子类中的所有属性,则当侦听器处理重新修补的事件时,这些属性的值将不正确。“

        2
  •  1
  •   hering Robyn Liu    15 年前

    您也可以这样发送活动:

    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 框架,其中事件也非常重要。

    推荐文章