Sebastian Müller
Sebastian Müller

Reputation: 5589

Interact with SWF Loader

I embedded a swf file in my application

<mx:SWFLoader source="@Embed(source='mod/VideoModule.swf')" width="50" height="50" id="loader" creationComplete="initLoader()" />

now with the help of the flex documentation I wanted to interact with my loaded swf by creating a SystemManager

[Bindable]
    public var loadedSM:SystemManager;

    private function initLoader() : void {
        trace(loader.content);
        loadedSM = SystemManager(loader.content);
        var b: Button = loadedSM.application["button1"] as Button;
        b.addEventListener(MouseEvent.CLICK, test);
    }

But when starting the application the error#1034 occurs and says that Main__embed_mxml_mod_VideoModule_swf_856293516@33f53c1 could not be converted into mx.managers.SystemManager

any ideas?

thanks in advance

Sebastian

Upvotes: 2

Views: 5875

Answers (2)

James Hay
James Hay

Reputation: 12720

So firstly i'd use the complete event of SWF loader ad the creationComplete event will fire when swf loader is created, not with it's contents have loaded.

<mx:SWFLoader source="@Embed(source='mod/VideoModule.swf')" width="50" height="50" id="loader" complete="loaderCompleteHandler(event)" />

Then i would also pass the FlexEvent argument when the event fires. That event gives you access to the instance of SWFLoader. SwfLoader then has a property called content which will give you access to the loaded swf. If the swf then exposes a property named button1 you could do something like:

private function loaderCompleteHandler(event : FlexEvent) : void 
{
    var swfLaoder : SWFLoader = SWFLoader(event.target);
    swfLaoder.content["button1"].addEventListener(MouseEvent.CLICK, test);
}

Upvotes: 3

RobDixon
RobDixon

Reputation: 104

James is right, though it could be simpler. You can start with a similar SWFLoader declaration, using the complete event:

<mx:SWFLoader source="@Embed(source='mod/VideoModule.swf')" 
    width="50" 
    height="50" 
    id="loader" 
    complete="swfLoaded(event)" />

Then you can reference the loader object directly in your handler (assuming the script is in the same MXML file as the SWFLoader declaration):

private function swfLoaded(event:Event):void
{
    loader.content['button1'].addEventListener(MouseEvent.CLICK, test);
}

Or if you want to use the SystemManager features, you can cast the content to a SystemManager and go from there:

var loadedSM:SystemManager;
private function swfLoaded(event:Event):void
{
    loadedSM = SystemManager(loader.content);
    var b:Button = loadedSM.application["button1"] as Button;
    b.addEventListener(MouseEvent.CLICK, test);
}

Of course you probably want to set up handlers for the SWFLoader's ioError and securityError events too, so things are handled gracefully in case of problems.

Upvotes: 0

Related Questions