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

 

senocular.com ActionScript Library

polygon.as

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
/* Example:
	polygon(this, 0,0, 0,100, 100,100, 100,50, 50,0, 0,0, [0, 0xFF, 100], [0xFF00, 100]);
*/

_global.polygon = function(mc, xs, ys, lstyle, fstyle){
	// any number of comma separated x,y positions
	// lstyle and fstyle are arrays of lineStyle and beginFill arguments respectively
	var len = arguments.length;
	if (typeof arguments[len-2] == "object"){
		lstyle = arguments[len-2];
		fstyle = arguments[len-1];
	}else if (typeof arguments[len-1] == "object"){
		lstyle = arguments[len-1];
		fstyle = false;
	}else{
		lstyle = false;
		fstyle = false;
	}
	if (lstyle) mc.lineStyle.apply(mc, lstyle);
	if (fstyle) mc.beginFill.apply(mc, fstyle);
	mc = arguments.shift();
	for (var i=0; i<len; i+=2){
		if (typeof arguments[i] == "number"){
			if (!i) mc.moveTo(arguments[i], arguments[i+1]);
			else mc.lineTo(arguments[i], arguments[i+1]);
		}else break;
	}
	if (fstyle) mc.endFill();
	return mc;
}