MovieClips and Object Scope in AS2
4th May, 2007 – 5:56 pmI am a relative new-commer to ActionScript 2 but I am picking things up quickly. One thing that tripped me up at first was understanding how MovieClips on the Main Stage relate to the clasess we write and the objects that they spawn.
My initial solution to the problem was to pass the MovieClip I wanted to work on to the object’s constructor, for example:
// Main Timeline Code, used to spawn the object.
import Player.as;
myPlayer = new Player(this);
The “Player” class’ constructor would then bring the object into localscope, in a bit of a round-about fashion:
class Player extends MovieClip {
/**
* Constructor, pass in the main stage to bring it into the scope of this object
* @param mc Stage MovieClip.
*/
public function AudioController(mc:MovieClip)
{
// Bring into scope and then make it disappear (how handy...)
stage = mc;
stage._visible = false;
}
}
Although this solution works (thank God - I was about to lose the will to live!), it's not exactly elegant. I spent a while Googlin' and reading through Essential AS2.0, but could not find what I was looking for. This was especially annoying becuase the answer is pretty simple.
So, to get to the point (and to hopfully help out some other AS 2.0 beginners), here is how you are "meant" to get MovieClips into scope! First of all, you need to set the "Linkage" properties for your movieclip from inside the Library (I wondered what that was for! ;) Set the AS2 Class Property to be the same as your classes' name, now you are ready to Get In Scope!
class ScopeTest extends MovieClip
{
/**
* Constructor, no need to pass anything into this, the Linkage
* Property will mean that it's automatically in Scope!
*/
function ScopeTest()
{
this._visible = false;
}
}
Fantastic! But this still requires there to be an instance of your MovieClip on the main stage / timeline. If you want to get Really dynamic about this, then we really want Flash to spawn / create the MovieClip for us! Maybe you've already tried
var mcTest:MovieClip = new MovieClip();
And been disappointed to find out that nothing happened. The reasoning behind it is a bit complicated for myself at the momnet, but our good friend Senocular does a great job of explaining it. It boils down to having to spawn your MovieClip as such:
// Spawn a new MovieClip without it having to already be on the Main Stage
var myInstance = __timeline__.attachMovie("__Linkage-Name__", "myInstance", __level__).init();
Where "__timeline__" is where you want your new MC to appear (eg: _root), "__Linkage-Name__" is the name you have given the Clip in the Linkage Properties Window (see above) and "__level__" is the level you wish the MC to appear on. Your "MyInstance" class should then look something like this:
class myClass extends MovieClip
{
// Constructor
function myInstance()
{
// no arguments passed
}
/**
* Fake Constructor which will return a new instance of this
* object.
*/
public function init(value1:Number, value2:Number):myClass
{
return this;
}
}