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

 

senocular.com ActionScript Library

asButton.as

Name: asButton() - [MX] make movieclip act like a button in frame rollover states
Author: senocular: www.senocular.com
Date: 1899-12-31T00:44:43.000
Documentation:
MovieClip ASBUTTON(): (Flash MX) makes a movieclip behave (framewise)
like a button symbol.  Normal state is frame 1, onRollover is frame 2
onPress is frame 3 with optional hit area with frame 4. Any previous
button events associated with the clip will remain after asButton is
called, however, reassigning new on actions used by asButton will
replace those set by asButton and take away from its functionality

Arguments:
- hit: (optional) boolean, determines whether or not to use frame 4 of the movieclip
as a hit area (similar to that of the hit frame in a button) as the active state of the
movieclip.asButton.  The way this is achieved is through a duplicated version of this clip
stopped on frame 4 and set as the original clips hitarea.  Be concious of what behaviors
might be carried over to the duplicated clip as its made which may cause complications in 
its application here.  Also be aware of how this duplicated clip will not maintain its re-
lationship with the original clip unless you tell it to.  Its name is this._name + "_hit"
- depth: (optional) user controlled depth of the hit duplicated clip.  If depth is not
passed, a check is made to see if a previous hit clip was made (clips name + "_hit") which if
existing, the old clip is replaced with the new hit in that existing depth, otherwise
a variable 'depth' in the _parent clip is used (and incremented) whether existant or not.

Applications:
- One use, to easily add onEnterFrame event recognition to a button.  Place the button on the
screen, and convert it to a movieclip symbol.  Set the clip with asButton and it will behave
exactly as it would as if it were still a button symbol. Any onPress, onRelease events etc.
should be added before asButton is called.  Since the symbol type IS a movieclip, it is already
able to recognise onEnterFrame events at this point (without the need of using something like
ASBroadcaster)

Example:
mc.onRollOver = function(){trace("over")}
mc.onPress = function(){trace("press")}
mc.asButton(true); // mc now acts as a button with rollover states (using frames 1-3 and 4 as hitstate)

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
MovieClip.prototype.asButton = function(hit, depth){
	var go1 = function(){this.gotoAndStop(1);};
	var go2 = function(){this.gotoAndStop(2);};
	var go3 = function(){this.gotoAndStop(3);};
	var old, comb = function(a,b){return function(){a.call(this);b.call(this);}};
	this.onRollOut		= (old = this.onRollOut)		? comb(old,go1) : go1;
	this.onReleaseOutside	= (old = this.onReleaseOutside)	? comb(old,go1) : go1;
	this.onRelease		= (old = this.onRelease)		? comb(old,go2) : go2;
	this.onRollOver		= (old = this.onRollOver)		? comb(old,go2) : go2;
	this.onDragOut		= (old = this.onDragOut)		? comb(old,go2) : go2;
	this.onPress		= (old = this.onPress)		? comb(old,go3) : go3;
	this.onDragOver		= (old = this.onDragOver)		? comb(old,go3) : go3;
	if (hit){
		if (!depth){
			if (this._parent[this._name +"_hit"]) depth = this._parent[this._name +"_hit"].getDepth();
			else depth = ++this._parent.depth;
		}
		var hit_mc = this.duplicateMovieClip(this._name + "_hit", depth);
		hit_mc._visible = false;
		hit_mc.gotoAndStop(4);
		this.hitArea = hit_mc;
	}
	if (this.hitTest(_root._xmouse, _root._ymouse, true)) this.onRollOver();
	else this.gotoAndStop(1);
}