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

2023-09-08 15:30:12 作者:多情不滥

我想从1级传递自定义事件数据到其他类...这是我的codeS。

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

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

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 {     进口对象类型:flash.events.Event;

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

}

我AP preciate任何帮助。

I appreciate any help..

推荐答案

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

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

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

"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."

 
精彩推荐