【问题标题】:AS3 Pass Custom Event Data QuestionAS3 通过自定义事件数据问题
【发布时间】:2010-08-09 09:37:26
【问题描述】:

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

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 { 导入 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); 
    }
}

}

感谢您的帮助..

【问题讨论】:

  • 如果您发布完整的课程定义会更容易帮助您...
  • 我现在明白了....我只需要在我的自定义事件中使用 bubble=true...

标签: apache-flex flash actionscript-3


【解决方案1】:

看不到您实际的自定义事件类,很难看出错误在哪里,但我猜您可能忘记覆盖 clone() 方法:

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

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

【讨论】:

  • 我刚刚在帖子中更新了我的自定义事件...我整晚没睡...现在我的头不在我身边了... ...虽然 +1
  • 所以我的猜测是正确的 - 在您的自定义事件中没有覆盖 clone() 方法。你应该试一试。公共覆盖函数 clone():Event { return new YouTubeSearchEvent(this.type, this.videoResult); }
  • 我添加了你的覆盖方法,但仍然没有运气......我的问题是我的 c.as 永远不会从 a.as 收到事件对象......只有 b.as可以收到它......奇怪......
【解决方案2】:

你也可以像这样调度你的事件:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 2021-09-15
    相关资源
    最近更新 更多