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

 

senocular.com ActionScript Library

align.as

Name: align() - aligns movieclips tops, bottoms, left, rights and/or centers.
Author: senocular: www.senocular.com
Date: 1899-12-31T00:56:24.000
Documentation:
MovieClip ALIGN: Aligns clips to this clip as done through the
align panel.  You can align left sides, right sides, bottoms,
tops, and centers either horizontally or vertically.
 
Arguments:
- h: (horizontal align) alignment horizontally.  This can be any one of three strings, "left",
"right", "center", or null (or anything other than those 3) for no alignment.
- v: (vertical align) alignment vertically.  This can be any one of three strings, "top",
"bottom", "center", or null (or anything other than those 3) for no alignment.
- mcs: (moveiclips) any number of sequentially added movieclips which are to be aligned
to this clip.
 
Note:
- alignment is done through any scope.

Example:
// align clips a, b and c to this clip when the mouse is pressed
// aligns all clips to the left of this.
onClipEvent(mouseDown){
	this.align("left", null, _root.a, b, _parent.container.c);
}
 
// align clips a, b and c to this clip when the mouse is pressed
// aligns all clips to the center vertically (along x axis)
onClipEvent(mouseDown){
	this.align(null, "center", _root.a, b, _parent.container.c);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
MovieClip.prototype.align = function(h,v, mcs){
	var i, l = arguments.length, b, coords;
	var tb = this.getBounds(_root);
	for (i=2; i<l; i++){
		ab = arguments[i].getBounds(_root);
		if (h=="left") arguments[i]._x += tb.xmin - ab.xmin;		// left align
		else if (h=="right") arguments[i]._x += tb.xmax - ab.xmax;	// right align
		else if (h=="center") arguments[i]._x += tb.xmin + this._width/2 - ab.xmin - arguments[i]._width/2;	// center align y axis
		if (v=="top") arguments[i]._y += tb.ymin - ab.ymin;		// top align
		else if (v=="bottom") arguments[i]._y += tb.ymax - ab.ymax;	// bottom align
		else if (v=="center") arguments[i]._y += tb.ymin + this._height/2 - ab.ymin - arguments[i]._height/2; // center align x axis
	}	
}