Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

attachMovie.as

Name: attachMovie w/ class (and Object.getRegisteredClass) [MX]
Author: senocular: www.senocular.com
Date: 1899-12-31T00:04:35.600
Documentation:
MovieClip ATTACHMOVIE rewrite: Allows the passing of a class
from which the attached movie instance will inherit from.
Also included in the mess is an Object.registerClass rewrite
which lets you check the current registered class with any
given linker ID using Object.getRegisteredClass

scope.attachMovie(id, name, depth, initObj, class, initArgs);
scope.attachMovie(id, name, depth, class, initArgs);

Arguments:
- id: (original) linkage ID of movieClip attached
- name: (original) new name for the movieclip attached
- depth: (original) depth at which the movieclip is attached
- initObj: (original, optional) init object for clip - an object of parameters which
are to be copied into the object upon its creation
- class: (new, optional) a class function which the newly attached movieclip is to be
associated with. Equivalent to Object.registerClasses theClass argument
- initArgs: (new, optional) an arguments array which is to be passed to the newly
attached clip's init method.  The init method you would have to create yourself.  It serves
as a type of manually called constructor.  Note: This is not to be confused with the initObj
whose properties are copied directly into the movieclip object in its creation (before any
constructor or init method is envoked).

Returns:
- the newly attached movieclip.


Object.getRegisteredClass(id)

Arguments:
- id: linker ID of a library item from which to get associated class

Returns:
- the class function associated with the linker ID passed.  If there is none, null is returned

Example:
// setup normal class for registerclass
normal = function(){this.kind = "virus";}
normal.prototype = new MovieClip();
normal.prototype.onEnterFrame = function(){
	trace(this._name +": "+ this.kind);
}

// superclass for two subclasses
child = function(){}
child.prototype = new MovieClip();
child.prototype.init = function(a,b,c){
	trace("init args "+ this._name +": "+ arguments)
	this.a = a;
	this.b = b;
	this.c = c;
}
child.prototype.onEnterFrame = function(){
	trace(this._name +": "+ this.kind +", "+ this.a + this.b + this.c);
}
// two subclasses to the super, each will be associated 
// with the attached clip via attachmovie
boy = function(){this.kind = "male";}
boy.prototype = new child();
girl = function(){this.kind = "female";}
girl.prototype = new child();



// start with the normal association with the normal class
Object.registerClass("clipID",normal);
this.attachMovie("clipID", "normal1_mc", 0);

// attach the boy and girl (same linkage ID) but with passed class
// argument - one with init object, one without, both with class init args
this.attachMovie("clipID", "boy_mc", 1, boy, [1,2,3]);
this.attachMovie("clipID", "girl_mc", 2, {_xscale:50, _yscale:50}, girl, [4,5,6]);

// the registered class of the linkage ID should be restored back
// to normal as it was originally defined as being
// another normal clip can then be attached
trace("regClass restored? "+ (Object.getRegisteredClass("clipID") == normal));
this.attachMovie("clipID", "normal2_mc", 3, {_x:200, _y:200});
trace("======================");


outputs:
init args boy_mc: 1,2,3
init args girl_mc: 4,5,6
regClass restored? true
======================
normal2_mc: virus
girl_mc: female, 456
boy_mc: male, 123
normal1_mc: virus
normal2_mc: virus
girl_mc: female, 456
boy_mc: male, 123
normal1_mc: virus
...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Object.getRegisteredClass = function(id){
	if (!this._registeredClasses || !this._registeredClasses[id]) return null;
	return this._registeredClasses[id];
}
Object.$registerClass = Object.registerClass;
Object.addProperty("registerClass",function(){return function(id, class){
		if (!this._registeredClasses){
			this._registeredClasses = {};
			ASSetPropFlags(this,"$_registeredClasses",1);
		}
		this._registeredClasses[id] = class;
		this.$registerClass(id, class);
}},null);
ASSetPropFlags(Object,["$registerClass","getRegisteredClass"],1);
MovieClip.prototype.$attachMovie = MovieClip.prototype.attachMovie;
MovieClip.prototype.attachMovie = function(id, name, depth, init, class, initArgs){
	if (typeof init == "function"){
		initArgs = class;
		class = init;
		init = undefined;
	}
	if (class){
		var oldClass = Object.getRegisteredClass(id);
		Object.registerClass(id,class);
		var mc = this.$attachMovie(id, name, depth, init);
		if (initArgs.length) mc.init.apply(mc, initArgs);
		Object.registerClass(id,oldClass);
		return mc;
	}else return this.$attachMovie(id, name, depth, init);
}
ASSetPropFlags(MovieClip.prototype,"$attachMovie",1);