Avg. Rating 3.0

Problem

Add to specified frames of loaded movie some function accessible to both clip and main app scope.

Solution

Use addFrameScript() function, use dynamic compiled function to refer main app vars and components.

Detailed explanation

Main idea of this recipe is to use undocumented AS3 function addFrameScript(). Here we have sample application project attached to this recipe with MovieClip loaded into application and added to it's display list (Gumbo project for 10-th player and 4-th sdk).

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo"
    xmlns="*"  xmlns:flash="flash.display.*"
    minWidth="1024" minHeight="768" creationComplete="init();">
    <fx:Script>
        <![CDATA[
           
           
            private var clip:MovieClip;
            private const DEFAULT_SCRIPT:String = "here is default string";
           
            public var referFunction:Function;
            public var referFrame:uint;
           
            private function init():void{
           
               
                referFunction = function():void{
                   
                    trace("original refer function called");
                    scriptArea.text = "original refer function called \n"+scriptArea.text;
               
                }
               
                var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);   
                var loader:Loader = new Loader();
           
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onClipLoaded);
                loader.load(new URLRequest("assets/movie.swf"),loaderContext);
            }
           
           
            private function onClipLoaded(evt:Event):void{
           
                clip = evt.target.content as MovieClip;
                clipHolder.addChild(clip);
               
                clip.play();
           
            }

            protected function btn_stop_clickHandler(event:MouseEvent):void
            {
                clip.stop();
            }
           
            protected function btn_add_clickHandler(event:MouseEvent):void
            {
                clip.addFrameScript(clip.currentFrame,createFunction(scriptArea.text, clip));
            }
           
            protected function btn_fps_clickHandler(event:MouseEvent):void
            {
                clip.addFrameScript(clip.currentFrame,function():void{
                   
                    if(clip.root.stage.frameRate > 1) clip.root.stage.frameRate -= 1;
                    else return;
                    trace('frame rate redused to '+clip.root.stage.frameRate);
               
                    });
            }
           
            protected function btn_orig_clickHandler(event:MouseEvent):void
            {
                clip.stop();
               
                referFrame = clip.currentFrame;
                clip.addFrameScript(referFrame,referFunction);
            }
           
            protected function btn_override_clickHandler(event:MouseEvent):void
            {
                clip.stop();
               
                referFunction = function():void{
                   
                    trace("override refer function called");
                    scriptArea.text = "override refer function called \n"+scriptArea.text;
               
                }
               
                clip.addFrameScript(referFrame,referFunction);
            }
           
            protected function btn_play_clickHandler(event:MouseEvent):void
            {
                clip.play();
            }
           
           
            public static function createFunction(code:Object,target:Object):Function{
               
                var f : Function =  function(...rest) : void {
                       
                        if((code==null)||(target == null)) return;
                       
                        trace('here we in frame '+(target as MovieClip).currentFrame+" and tracing "+code.toString());
                  
                       
                    };
               
                return f;
           
           
            }

        ]]>
    </fx:Script>

<mx:UIComponent id="clipHolder" width="100" height="100" top="10" left="10" />

<s:Button id="btn_stop" label="click to stop clip" left="150" top="10" click="btn_stop_clickHandler(event)"/>

<s:Button id="btn_add" label="click to add trace" left="150" top="40" click="btn_add_clickHandler(event)"/>
<s:Button id="btn_fps" label="click to add fps-=1" left="150" top="62" click="btn_fps_clickHandler(event)"/>
<s:Button id="btn_orig" label="add original function" left="150" top="84" click="btn_orig_clickHandler(event)"/>
<s:Button id="btn_override" label="override function" left="150" top="106" click="btn_override_clickHandler(event)"/>

<s:Button id="btn_play" label="click to play clip" left="150" top="138" click="btn_play_clickHandler(event)"/>

<s:TextArea id="scriptArea" width="300" height="100" top="10" left="300" text="{DEFAULT_SCRIPT}" />
</s:Application>

 

In this application we have 6 buttons - first one stops loaded movie timeline, last one resumes it. it's also a group of 4 buttons to add into current frame code for tracing TextArea text, for redusing loaded clip fps value by - each time this frame played, for adding into current frame dynamically compiled function and for overriding this function.

Please put Your attention on fact that in 2 dynamilically compiled functions TextArea instance is scoped so this is a way to access outer elements and vars from loaded Movie Clip (clip created in Flash CS4compiled with 'sccess network only' security settings, for 10-th player and AS3).

Hope You will use this dao for good only!

Report abuse

Related recipes