开发者

Flex Event Dispatcher Questions

I am trying to dispatch an custom YouTubeEvent from my Player.as and wish my Main.as would listen and create the video player...Apparently my eventHandler can't catch the event to create the videoplayer.......My flex debug mode is so screw up I can't even use it...My code is as follow..I really appreciate any reply or help.....

My custom event..

package com.youtube.events {
    import flash.events.Event;

    public class YouTubeEvent extends Event{

        public static const PLAYER_READY:String="PLAYER_READY";

        public function YouTubeEvent(type:String){
            super(type);
        }
    }

}

My Main.as

public class SearchYoutube extends Sprite
{
      private var videoPlayer:Player;

  public function SearchYoutube()
    {

    /*********************Load Video Player****************************/
        loadPlayer();
    }


    private function loadPlayer():void{
    videoPlayer= new Player();
    videoPlayer.addEventListener(YouTubeEvent.PLAYER_READY, playerReady);

            //playReady would never be excuted....
    }

            private function playerReady(event:YouTubeEvent):void{
    videoPlayer.createPlayer();   //This handler would never be executed...
    addChild(videoPlayer);       //This handler would never be executed...
    }

}

Player.as

//only show part of codes here
public function Player(){

}
public function createPlayer():void{

    _loader = new Loader();
    _loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);


    }

  开发者_JAVA技巧  private function onLoaderInit(event:Event):void {

    _loader.content.addEventListener("onReady", onPlayerReady);

            }


    private function onPlayerReady(event:Event):void {

    dispatchEvent(new YouTubeEvent(YouTubeEvent.PLAYER_READY));

    }


YouTubeEvent.PLAYER_READY is dispatched some time after calling createPlayer(). You should call createPlayer() after videoPlayer.addEventListener(YouTubeEvent.PLAYER_READY, playerReady):

private function loadPlayer():void
{
    videoPlayer= new Player();
    videoPlayer.addEventListener(YouTubeEvent.PLAYER_READY, playerReady);
    videoPlayer.createPlayer();
}


This short tutorial will get you on the right path on using custom events with Flex.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜