[mtasc] Subclassing MovieClip via __proto__ and attachMovie

Ralf Bokelberg info at bokelberg.de
Tue Dec 20 12:16:06 CET 2005


Hi Simen

You could try it like this, but normally i would use composition for the 
main timeline and Object.registerClass for the other clips.


class Test extends MovieClip {
	
	public static function assimilate( target:MovieClip):Test{
		target.__proto__ = new Test.prototype;
		target.init();

		var result = target;
		return result;
	}
	
	private var x:Number;
	private var tf:TextField;
	
	public function init(){
		x = 7;
	}
	
	public function method(){
		trace("method " + x);
		this.createTextField("tf", 1, 0, 0, 100, 100);
		tf.text = "method " + x;
	}
}

class MainClass {
	
	private static var app:Test;
	
	public static function main( timeline:MovieClip){
		app = Test.assimilate( timeline);
		app.method();
	}
}

Cheers,
Ralf.




Simen Brekken wrote:

>>Hi Simen,
>>
>>
>>>I've been playing around with assimilating MovieClips using  
>>>__proto__ but I'm having difficulties with normal MovieClip methods  
>>>like attachMovie. In my Slideshow class it seems it stops behaving  
>>>like a Slideshow after it's constructor is run.
>>
>>You should also update the __constructor__ property...
> 
> 
>>private function Slideshow(target:MovieClip) {
>>    target.__proto__ = this.__proto__;
>>    target.__constructor__ = this.__constructor__;
>>    this = Slideshow(target);
>>}
> 
> 
> Why is that necessary? The constructor is already run by the time that
> statement is run so how can that have any effect?
> 
> 
>>I'm also not entirely sure that the cast of target to Slideshow will  
>>work. Can't remember if casts check the prototype chain (likely) or  
>>whether they use some other magical method to validate the cast.
> 
> 
> That's the problem I've been trying to overcome, that methods stop existing
> after the constructor.
> 
> 
>>Either way, I'd suggest you'd be better of using composition rather  
>>than inheritance, especially when working with MovieClips. It's so  
>>frustrating that you cannot override (i.e. put a getter/setter in the  
>>way of) certain properties of a MovieClip such as _width.
> 
> 
> Composition is fine but this is an experiment to try and overcome the pretty
> large overhead composition requires in terms of tracking both scope and
> container everywhere.
> 
> 
> 
> --
> MTASC : no more coffee break while compiling




More information about the mtasc mailing list