Useful Flash custom functions

If you code a lot with the same language, no matter if Java, Php or Actionscript, sooner or later you will find yourself writing the same code you already wrote in a previous project.

This is extremely time-wasting, because forces you to spend your time writing routines you have already written some time ago and does not let you totally focus on the script.

That’s why most programmers develop custom functions performing tasks they have to solve in almost every project

Andre Marianiello decided to share with us some useful Flash custom functions.

This is what he sent me by email:

I have compiled some useful functions for flash, and I was wondering if you would like to see them or put them on your blog for your readers. I can provide the .as file with comments and create examples if you want.

These functions include:

- line collision detection
- a useful replacement for the Object.toString() method
- cloning function to copy Objects and Arrays
- plus some trig functions that are often used in your tutorials

All functions come in a .as file plenty of comments. Let’s see it:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Object.prototype.toString = function() {
	//set empty string to be displayed
	var string = "";
	//cycle through properties and functions
	for (var i in this) {
		//if not a function
		if (typeof (this[i]) != "function") {
			//add the property name and value to the string
			string += i+":"+this[i].toString()+", ";
		} else {
			//else add the function name and "Function"
			string += i+":Function, ";
		}
	}
	//don't include last comma and space in final string
	string = string.substring(0, string.length-2);
	return string;
};
MovieClip.prototype.lineHitTest = function(x1, y1, x2, y2, pre) {
	//find difference in x
	var x = x1-x2;
	//find difference in y
	var y = y1-y2;
	//find distance between points
	var dis = Math.sqrt(x*x+y*y);
	//when pre(precision) equals 2, it checks every 2 points
	//therefore, the number of point equals dis/pre
	var num = dis/pre;
	//vars to hold current point
	var tx, ty;
	for (var i = 1; i<=num; i++) {
		//if pre equals 2 and distance is ten, then 
		//we find a point 1/5 of the way down the line.
		tx = x2+i*x/num;
		ty = y2+i*y/num;
		//if the point(tx, ty) touches the movieclip we're testing
		if (this.hitTest(tx, ty)) {
			//return true because the movieclip is on the line
			return true;
		}
	}
	//return false because none of the tested points hit the movieclip
	return false;
};
Object.prototype.clone = function() {
	//empty object
	var temp = new Object();
	//for each propety in the going-to-be-cloned object
	for (i in this) {
		//copy property name with value to new object
		temp[i] = this[i];
	}
	return temp;
};
Array.prototype.clone = function() {
	//empty array
	var temp = new Array();
	//for each value in the going-to-be-cloned array
	for (i in this) {
		//copy value with index to new object
		temp[i] = this[i];
	}
	return temp;
};
angleFromTo = function (x1, y1, x2, y2) {
	//find change in x
	var x = x2-x1;
	//find change in y
	var y = y2-y1;
	//Math.atan2 returns angle in radians; Math.PI/180 converts to degrees
	return Math.atan2(y, x)/(Math.PI/180);
};
distanceFromTo = function (x1, y1, x2, y2) {
	//find change in x
	var x = x2-x1;
	//find change in y
	var y = y2-y1;
	//use pythagorean theorem to find distance
	return Math.sqrt(x*x+y*y);
};
vectorToHorz = function (ang, dis) {
	var rads = ang/(180/Math.PI);
	//returns x-component of a vector of dis magnitude at ang angle
	return dis*Math.cos(rads);
};
vectorToVert = function (ang, dis) {
	var rads = ang/(180/Math.PI);
	//returns y-component of a vector of dis magnitude at ang angle
	return dis*Math.sin(rads);
};

Andre also provided three examples to demonstrate how to use the functions

Let’s see the actionscript and the results:

Example 1

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "customFunctions.as"
arrow.onEnterFrame = function() {
	this._rotation = 0;
	var ang = angleFromTo(0, 0, this._xmouse, this._ymouse);
	var dis = distanceFromTo(0, 0, this._xmouse, this._ymouse);
	xm.text = (this._xmouse);
	funcbox.text = "vectorToHorz("+Math.round(ang)+", "+Math.round(dis)+")";
	vth.text = Math.round(vectorToHorz(ang, dis));
	ym.text = (this._ymouse);
	funcbox2.text = "vectorToVert("+Math.round(ang)+", "+Math.round(dis)+")";
	vtv.text = Math.round(vectorToVert(ang, dis));
	this._rotation = ang;
};

Example 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "customFunctions.as"
ball.onEnterFrame = function() {
	x1 = Math.random()*Stage.width;
	y1 = Math.random()*Stage.height;
	x2 = Math.random()*Stage.width;
	y2 = Math.random()*Stage.height;
	hit = this.lineHitTest(x1, y1, x2, y2, 4);
	_root.clear();
	_root.lineStyle(3, 0x209702);
	_root.moveTo(x1, y1);
	_root.lineTo(x2, y2);
	if (hit) {
		hitbox.text = "hit";
	} else {
		hitbox.text = "no hit";
	}
};

Example 3

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
#include "customFunctions.as"
num.onChanged = function() {
	testObj.num = this.text;
	updateString();
};
str.onChanged = function() {
	testObj.str = this.text;
	updateString();
};
but1.onRelease = function() {
	testObj.bool = true;
	updateString();
};
but2.onRelease = function() {
	testObj.bool = false;
	updateString();
};
updateString = function () {
	string.text = testObj.toString();
};
 
testObj = {num:5, str:"test", bool:true};
num.text = testObj.num;
str.text = testObj.str;
num.restrict = "0-9";
updateString();

Sharing custom functions is really a good idea… do you use custom functions in your scripts? Do you want to share them? It would be nice to have a library full of useful custom functions.

This is the zipped archive sent me by Andre, give him feedback

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 7 comments

  1. Keith Peters

    on November 10, 2007 at 3:29 pm

    While it is nice to have utility functions like this, the use of prototype like this has been discouraged since about 2003. Much better to make a utils class in AS2 or AS3, or a class that extends MovieClip or Sprite and adds these methods.

  2. mike

    on November 10, 2007 at 7:56 pm

    Actually there are lot of ready and handy prototypes, frameworks, libraries, etc… Simply check wis AS2.0, CASA framework, GuggaFramework, proto.layer51.com and so on and so on… You will find a lot of useful routines and classes.

  3. Thomas

    on November 11, 2007 at 12:46 am

    wicked!!!

  4. Dog Gon Mad

    on November 11, 2007 at 5:47 am

    Hey, great functions, i have been watching this site for a while and all of your tutorials are great, thanks.

  5. Scott

    on November 22, 2007 at 7:08 am

    Wow, Flash MX called, they want their code back. Kidding of course. These are useful methods but as Keith pointed out very, very, very dated. You’ll want to drop prototype and use your new friend, extends. Or if you really want to reuse utility type methods create a utils class(es) and set up your ide of choice to include the class paths in all your projects, then you simply have to import what ever utils you want for your particular project.

  6. Kenneth Camilleri

    on October 1, 2008 at 10:00 am

    The idea on custom functions making everything simipler in AS 2 and AS 3 is excellent. Like this people won’t have to stay figuring out a load of stuff, as long as they know what the function does.

    I rate this 5, absolutely excellent. This is what I’ve been looking for all along too — To make an arrow point towards the mouse kind of set of functions.

  7. ?? ??????? ???? ? Flash ( ??????? ??) ?????? ??????? | maque.org.ua

    on February 16, 2010 at 10:03 am

    [...] ActionScript Functions 2.Useful Flash custom functions 3.How to create a custom function VN:F [1.8.1_1037]please wait…Rating: 0.0/10 (0 votes cast)VN:F [...]