开发者

Flex: weird question, can't access application property in init event of SWFLoader

开发者 https://www.devze.com 2022-12-14 23:08 出处:网络
Very weird problem, I am new to AS, but I have to say it\'s hard to comprehend AS is a modern language:

Very weird problem, I am new to AS, but I have to say it's hard to comprehend AS is a modern language:


            private function completed():void
            {               
                trace("completed.");                
                var player:Object = (loader.content as SystemManager).application as Object;
                player.playVideo();
                player.setSize(200,300);
            }


    SWFLoader id="loader" horizontalCenter="0" width="100%" height="362" source="http://localhost:8000/开发者_运维知识库testflv1.swf" init="completed()"

What I want to do is embed a swf in another parent swf, and call a function from parent to the embedded swf. Then, I use the init event for the SWFLoader to invoke the method. Before doing this I have verified that using the same code in a button click handler, it is fine with:

var player:Object = (loader.content as SystemManager).application as Object;

But if in a init event handler, the (loader.content as SystemManager).application is a null.

Whereas, in the document of SWFLoader:

init Dispatched when the properties and methods of a loaded SWF file are accessible.


I think the problem is here because the application property of your included file is not yet initialized. You can listen to the APPLICATION_COMPLETE event, which will tell you when the application property of your SWF content is completed.

        public var loadedSM:SystemManager;

        private function init():void
        {                               
            trace("init.");                            
            loadedSM = SystemManager(loader.content);
            loadedSM.addEventListener(FlexEvent.APPLICATION_COMPLETE, callFunc);                
        }

        private function callFunc(event:FlexEvent):void
        {
            LoadFileInclude(loadedSM.application).playVideo();
        }

    ]]>
</mx:Script>
<mx:SWFLoader id="loader" horizontalCenter="0" width="100%" height="362" source="LoadFileInclude.swf" creationComplete="init()" />  


I had this same problem. Googling around forever and finally found an answer that led me to the solution buried in this Old Nabble thread.

Basically, when the SWF is injected, the MovieClip itself is in a different place, and it's kind of a pain to get it out. What I did:

MXML:

    <mx:SWFLoader
       id="loader"
        source="@Embed(source='/flash/preloader.swf')" />
AS:
    var mc:MovieClip = Loader(DisplayObjectContainer(swf.content).getChildAt(0)).content 
            as MovieClip;
    if(mc)
    {
        mc.callAFunction();
    }

0

精彩评论

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

关注公众号