Hi!
Have you ever found yourself using one function that you wrote many times? Then chances are it’s useful. So post some of them here so that other users may benefit from them. I’ll start with some.
easeTo(x, y, speed) (MovieClip)
Makes the mc go to x, y and does so with an easing motion. Speed is, well, speed and should be a number between 0 and 1 (although you get some pretty cool effects when it’s between 1 and 2 also). Useful for moving stuff around.
example use: mymovieclip.easeTo(200, 100, 0.3)
MovieClip.prototype.easeTo = function(x, y, speed) {
this.onEnterFrame = function() {
this._x +=(x-this._x)*speed;
this._y +=(y-this._y)*speed;
if ((Math.abs(x-this._x)<0.3)&&(Math.abs(y-this._y)<0.3)) {
delete this.onEnterFrame;
}
}
}
easeSize(width, height, speed) (MovieClip)
Similar to easeTo, except it changes the height and width of a movieclip and does so in an easing movement. Useful for gradually transforming the height and width of boxes so that images fit in them and whatnot. Speed should be between 0 and 1, but if you put it between 1 and 2, you get a nice springy effect.
MovieClip.prototype.easeSize = function(new_width, new_height, speed) {
this.onEnterFrame = function() {
this._width += (new_width-this._width)*speed;
this._height += (new_height-this._height)*speed;
if
((Math.abs(new_width-this._width)<0.4)&&(Math.abs(new_height-this._height)<0.4)) {
delete this.onEnterFrame;
}
}
}
goBackwards(num_frames) (MovieClip)
goForward(num_frames (MovieClip)
These two make a movieclip play backwards or forwards by a designated amount of frames (num_frames). Useful for all sorts of stuff, especially nice animated buttons where you want the rollOut animation to continue where the rollOver animation left off.
MovieClip.prototype.goBackwards = function(frames) {
this.onEnterFrame = function() {
this.gotoAndStop(this._currentframe - 1);
frames -= 1;
if (frames == 0) {
delete this.onEnterFrame;
}
}
}
MovieClip.prototype.goForward = function(frames) {
this.onEnterFrame = function() {
this.gotoAndStop(this._currentframe + 1);
frames -= 1;
if (frames == 0) {
delete this.onEnterFrame;
}
}
}
stringToNumArray(input_string,delimiter)
Converts a string that has numbers seperated by delimiters in it (for example “39, 33, 22, 33” or “39 33 22 33” or “39|33|22|33”) and returns an array of those numbers, converted into number type and ready for use. For example if we have a string “89 22 33 454 33” the function will return an array that will contain [89, 22, 33, 454, 33].
stringToNumArray = function(myString, myDelim) {
myTextArray = myString.split(myDelim);
myNumArray = new Array();
for (i=0;i<myTextArray.length;i++) {
myNumArray* = Number(myTextArray*);
}
return myNumArray;
}