Targeting movieclip from an AS3 class file
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. ![]()

I personally would avoid creating static vars like that. If the object isn’t inside the class a general rule to follow would be that the class shouldn’t have access to it. Instead your class should dispatch an event, which can be listened to, to update objects elsewhere.
I see you have a Control class here. We would usually create a View and Controller. Our controller has a reference to the View, and the View has methods the controller can invoke. When these methods are invoked, the View updates itself.
I explored some of this… not beign in a document class complicates stuff sometimes… read this post I made having troubles with the as3 “attachmovie” (new instanceName()) and it’s sollution. in the third post.
http://www.flashfocus.nl/forum/showthread.php?t=39955
as it is in dutch.. the clue of the attachMovie sollution comes down to this.
public function AttachMovie(id:String):Class
{
return Class(Object(_loader.contentLoaderInfo).applicatio nDomain.getDefinition(id));
}
As Tink says, it’s normally better to encapsulate the functionality of each class using events and public methods instead of exposing the internals.
For this simple example, you might rewrite it something like…
Test.as
>>
package
{
import flash.display.MovieClip;
public class Test extends MovieClip
{
private var blackbox_mc:BlackBox;
public function Test()
{
blackbox_mc = new BlackBox();
this.stage.addChild(blackbox_mc);
var myTest:Control = new Control(this);
}
public function positionBlackBox(x=null,y=null) : void {
blackbox_mc.x = (x==null)?blackbox_mc.x:x;
blackbox_mc.y = (y==null)?blackbox_mc.y:y;
}
}
}
<>
package
{
import flash.display.MovieClip;
public class Control
{
private var view:MovieClip;
public function Control(mc:MovieClip)
{
view = mc;
view.positionBlackBox(null,100);
}
}
}
<>
package
{
import flash.display.MovieClip;
public class Test extends MovieClip
{
public function Test()
{
var myTest:Control = new Control(this);
}
public function positionBlackBox(x=null,y=null) : void {
blackbox_mc.x = (x==null)?blackbox_mc.x:x;
blackbox_mc.y = (y==null)?blackbox_mc.y:y;
}
}
}
<<
As the blackbox_mc is already on the views stage flash auto creates the reference, so you can just use blackbox_mc without creating it.
[...] is a reply to Brian Chau’s post here, but my comment didn’t go through too well, so I’ll repeat it [...]
wow, that broke! note to self avoid < and > in comments you can’t preview
I’ve posted it on my blog instead, you may want to remove the mess above…
http://www.dustypixels.com/blog/2008/01/15/targeting-movieclip-from-an-as3-class-file-reply/
Thanks guys for all your inputs.
I saw a blog you had back in August 07 about the HTC homescreen plugin (screenshots included), I thought it was very impressive. I just purchased an AT&T Tilt and would like to have that same interface. I have downloaded the relevant files (at least I think so), but I cannot seem to get it EXACTLY like your screenshots. I still get the greyish background instead of the crisp black that you have. I have tried a few things but to no avail. Could you please tell me the EXACT files and steps you took to get that. I would greatly appreciate it.
TC
TC, I used the Glossy Black Bar cab to change the default grey bars to glossy black bar. The Cab is for HTC Hermes. Not sure if it would work for Kaiser (Tilt). You can download it at:
http://rapidshare.com/files/86394842/Glossy_Black_Bars.cab.html
Alternatly, head to the XDA forum. There are a few Kaiser ROMs out there with black themes.
http://forum.xda-developers.com/forumdisplay.php?f=378