R – Flash Buider project can’t listen the event in a Flash CS4 project

actionscriptapache-flexflash

I am developing a project in Flash Builder which will load a file built by Flash CS4. The code in Flash CS4 is below, it's a doc class. I am sure the dispatchEvent has been invoked:

package {
import flash.display.MovieClip;
import flash.events.Event;

public class flashcs extends MovieClip
{
    public function flashcs():void
    {
        dispatchEvent(new Event("onPlayerLoaded", true));
    }

    public function playVideo():void
    {
        return;
    }
}

}

In Flash Builder, I use below code to listen to the event, but the callback never get called:

            private function playerLoaded():void 
        {
            Player = MovieClip(Loader.content)
            Player.addEventListener("onPlayerLoaded",Callback);
        }
mx:SWFLoader id="Loader" source="http://localhost/flashcs.swf" init="playerLoaded()"

I think maybe I can only listen to a SystemManager but not on the MovieClip? Because I have one prior project, the loaded swf file is built by Flash Builder, I was listening to the SystemManager without any trouble.

Thanks.

Best Answer

Try within the complete event not the init, if the dispatchEvent have been fired before you have been able to listen to it you will not get it.

<mx:SWFLoader id="Loader" source="http://localhost/flashcs.swf" complete="playerLoaded()"/>

Edit:

In your loaded class delay the dispatch event to fired it when object is added to the stage:

package {
 import flash.display.MovieClip;
 import flash.events.Event;

 public class flashcs extends MovieClip
 {
    public function flashcs():void
    {
        addEventListener(Event.ADDED_TO_STAGE, ready);
    }

    private function ready():void {
     removeEventListener(Event.ADDED_TO_STAGE, ready); 

     dispatchEvent(new Event("onPlayerLoaded", true));
    }

    public function playVideo():void
    {
        return;
    }
 }
}
Related Topic