Ok, well, I haven’t seen a thread like this aroung anywhere so I thought I’d start one. Basically, you just list, and whack up a code wih the best actionscripting practices and/or most efficient.
Feel free to add on to the list, or provide a faster method of a code given;).
My first example,
Function: searching through an array
Type: the (presumed) quickest method
Comment: The (presumed) fastest way to search an array for a certain value
Array.prototype.find = function(x) {
for (var i = this.length; i>-1; i--) {
if (this* == x) {
return true;
}
}
return false;
};
var myArray:Array = ["one", "two"];
trace(myArray.find("one"));
trace(myArray.find("three"));
Function: Key presses
Type: the (presumed) quickest method
Comment: UseASCii codes rather than Key.Key Name
So not,
Key.isDown(Key.SPACE)
but
Key.isDown(32)
Function: If/Else Strings
Type: the (presumed) quickest method
Comment: Try to keep all your conditions in a string, in the order of the one most likely to be met first, the one most likely to be met second… etc…
Function: Increasing a variable by one
Type: the (presumed) quickest method
Comment: Try to keep use variable++ or variable-- as oppose to variable += 1 and variable -= 1.
Function: Breaking for/while loops
Type: the (presumed) best practice
Comment: Whenever you are using a for or while loop, if you have any conditionals within it, break or return it after the condition is met.
Function: Declaring variables
Type: the (presumed) best practice, the (presumed) quickest method
Comment: Declare them as a variable, and with a datatype. So;
var myVar:Number = 6;
should be processed faster than
myVar = 6;
Function: onEnterFrames
Type: the (presumed) best practice, the (presumed) quickest method
Comment: Do not be an onEnterFrame fanatic, chances are if you get addicted to them, you will use them where not necessary. ONLY use them when necessary, they use enough CPU power already
Function: Referencing the _root frame.
Type: the (presumed) best practice
Comment: Try not to use _root at all throughout your movie. Try to use _global, _parent or put this sort of thing on the frame
var root:MovieClip = this;
myMovieClip.onPress = function(){
root.gotoAndStop(2);
}
This will save you a LOT of issues, with regard to referencing when you are going to be using loadMovie or you transfer the frame into a movie clip… etc…
Function: Where to put your codes…
Type: the (presumed) best practice
Comment: Try to keep all your coding on frames rather than movieClips… this makes things a lot more flexible for you to do.
Note: It also makes things quicker if it’s all in one place, so that flash doesn’t have to cycle through movie clips.
Function: Variable names
Type: the (presumed) best practice, the (presumed) quickest method
Comment: Try to keep all your variable’s names short and meaningful.
var mymassivevariablenamegoeshere:String = "hey";
is slower than
var longVar:String = "hey";
Function: Performing calculations
Type: the (presumed) best practice, the (presumed) quickest method
Comment: Do your best to perfom calculations only once whereever possible. It’s faster to have
var myNum:Number = 70005/54667;
onEnterFrame = function():Void {
trace(myNum*2);
};
rather than
onEnterFrame = function():Void {
trace((70005/54667)*2);
};
Doing this also makes things a lot easier to understand and process.
**Function:** Frame rate
**Type:** the (presumed) best practice
Comment: Use a nice, smooth frame rate, about 20-30 FPS, but really, it's whatever your happy with.