开发者

AS3 Pass Custom Event Data Question

开发者 https://www.devze.com 2023-01-10 15:04 出处:网络
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{

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

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

        }

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

}

I appreciate any help..


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

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


You could also dispatch your event like this:

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

You need a listener to notice if the event is dispatched. So you need something like

searchContainer.addEventListener(YouTubeSearchEvent.CHANGE_VIDEO_READY,onChangeVideoReady);

If the event is now dispatched the method onChangeVideoReady(event:YouTubeSearchEvent) is called. There you have access to event.videoId

Maybe you have a look at the mate framework, where events are also very important.

0

精彩评论

暂无评论...
验证码 换一张
取 消