views:

30

answers:

2

Hi guys... 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);


    }

    private function onLoaderInit(event:Event):void {

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

            }


    private function onPlayerReady(event:Event):void {

    dispatchEvent(new YouTubeEvent(YouTubeEvent.PLAYER_READY));

    }
+1  A: 

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

thelost
Thanks for the link......
Jerry
+2  A: 

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();
}
Maxim Kachurovskiy
Cool.thanks for the answer......
Jerry