Helpful stuff on Flash, ActionScript, After Effects etc

cases, code, tips and guidance

ActionScript 3.0: calling a function in the Document Class from a dynamically added object

without comments

in this example I will add a MovieClip from the library to the Stage.
this MovieClip contains an animation, and – in the last frame – this ActionScript:

stop();
(root as Object).slideEnded();

this code activates a function in the Document Class, smart!
here’s the Document Class where the TestMC MovieClip is added and the function later called from that MovieClip:

package {
         
          import flash.display.MovieClip;

          public class testContent extends MovieClip {
                   
                    private var _testHolder;
                    private var _testMC:TestMC;
                                       
                    public function testContent() {
                              init();
                    }
                   
                    private function init():void {
                              trace("function init");
                              setupTest();
                    }
                   
                    private function setupTest():void {
                              _testHolder = new MovieClip();
                              _testMC = new TestMC();
                              _testHolder.addChild(_testMC);
                              addChild(_testHolder);
                    }
                   
                    public function testEnded():void {
                              trace("testEnded!");
                    }
          }
}

in this example a function in the Document Class is called, actually the same can be achieved using an eventListener.

here’s a very similar example, first the ActionScript on the final frame of the TestMC MovieClip:

stop();
dispatchEvent(new Event("testEnded"));

and here, the ActionScript in the Document Class:

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

          public class testContent2 extends MovieClip {
                   
                    private var _testHolder;
                    private var _testMC:TestMC;
                                       
                    public function testContent2() {
                              init();
                    }
                   
                    private function init():void {
                              trace("function init");
                              setupTest();
                    }
                   
                    private function setupTest():void {
                              _testHolder = new MovieClip();
                              _testMC = new TestMC();
                              _testMC.addEventListener("testEnded", testDone, false, 0, true);
                              _testHolder.addChild(_testMC);
                              addChild(_testHolder);
                    }
                   
                    public function testDone(e:Event):void {
                              trace("testEnded!");
                    }
          }
}

useful? hopefully :)
both of the examples (.fla and .as) can be downloaded here:
examples of how to call a function in the Document Class from a dynamically loaded object

Bookmark and Share

Written by admin

december 10th, 2009 at 8:11 am

Leave a Reply