[mtasc] Unable to use _root when loaded into a container

Rudolf Vavruch rudolf at redshift.co.za
Mon Oct 2 17:59:27 CEST 2006


Alvin Hung wrote:
> 
> Hi,
> 
> I am using swfmill and mtasc to generate a SWF file named Foo.  At 
> runtime, I need to load Foo into another SWF file named Container.  As a 
> result, I cannot use _root in my code in Foo.  In this situation, what 
> is the right way to access the timeline inside my code in Foo?  Is there 
> a way to refer to the timeline in the current SWF file?
> 
> I realize I can have my code in Foo refer to a global function such as 
> _global.getFooMC(), but I would rather not create such a coupling.  I 
> want to make Foo pretty flexible.
> 
> Thanks.
> 

Hi,

Read this:
http://aralbalkan.com/360 (Assimilating _root in FAME)

Although he uses it to "assimilate" _root you can use the same technique 
to "assimilate" any movieclip into an Object.

And any SWFs that are loaded into other SWFs get to run their very own 
main(). The first argument of main() (appears automagically) is the 
"_root" of your SWF.

So in the main class of the SWF Foo have:

<code>
// this is needed for mtasc -strict when setting scope
private var __constructor__:Object;

static function main (scope:Foo) {
	var foo:Foo = new Foo(scope);
}

private function Foo (scope:Foo) {
	// Assimilate the target
	scope.__proto__ = this.__proto__;
	scope.__constructor__ = Foo;
	this = scope;

	// now foo refers to the root timeline of the SWF Foo
}
</code>

You can use "foo._parent" to refer to Container should you need to.

-Rudolf



More information about the mtasc mailing list