Now that more and more flash developers are writing AS3 as class file, rather than codes sitting in the timeline, it is important to know how to target movieclip from within an AS3 class file.
For document class file, that is easy and is similar to how to target instance from within the main timeline. So if there is a movieclip named myClip_mc, then it is targeted simply as myClip_mc or this.myClip_mc in the .as document class file.
But what if the .as file is not the document class file, but simply a class used by the document class?
Now try the following:
1. Create a Test.fla and a movieclip symbol named “BlackBox” in the movie library.
2. At the symbol properties panel, turn on Export for Actionscript. The class name would be “BlackBox”.
3. Specify the document class (Test) in the properties panel.
4. Create the Test.as document class with the following code. Note the use of the static variable.
package
{
import flash.display.MovieClip;
public class Test extends MovieClip
{
public static var blackbox_mc:MovieClip = new BlackBox();
public function Test()
{
this.stage.addChild(blackbox_mc);
var myTest:Control = new Control();
}
}
}
5. Create the Control.as class file with the following code:
package
{
public class Control
{
public function Control()
{
Test.blackbox_mc.y = 100;
}
}
}
Now the above technique would work. As noted above the trick is to create a static variable so that it can be referenced from the Control.as file.
But what if the movieclip instance is not programmatically created but instead is created by dragging the symbol from the library window? I don’t know the answer yet. If anyone has some suggestion, please share it here. ![]()
