Photoshop Tutorials, Flash Tutorials, 3Ds Studio Max Tutorials, Web Design Tutorials |
Home | Submit Tutorial | Top Sites | Free Templates (NEW) | Website templates | Privacy Policy | Link Exchange | Contact Us |
|
Welcome To ProDesignHost.com Flash Tutorials Area - ActionScript Programming Tips - Efficiency
One of the misconseptions of programming, is that even though if a piece
of code get's something done, it isn't allways the right way. In this series we're
taking a look at the efficiency of scripts. The more efficent your scripts are,
the faster they are executed in the Flash player. In small projects this can only
be a few millisecons faster, but in projects where a lot of calculations need
to be calculated per frame, this can really give a boost to the frame rate. For
example: a badly scripted 3D engine (a project where LOADS of calculations need
to be made) can get a framerate of like 10fps. While a optimised 3D engine, based
on the same mathematics can get a framrate of 80fps.
Minimising Calculations The smaller the amount of calculations that need to be calculated, the faster the script. Sounds logical eh? This is fairly simple to implement. You can save a frequently used value in a variable so it doesn't have to be calculated each time. For example, take a look at both of these preloader scripts:_root.onEnterFrame = function () { bt = _root.getBytesTotal(); bl = _root.getBytesLoaded(); if (bt == bl) { gotoAndPlay(2); } } //And this one: bt = _root.getBytesTotal(); _root.onEnterFrame = function () { bl = _root.getBytesLoaded(); if (bt == bl) { gotoAndPlay(2); } } The second script is the most efficent because it saves the total bytes of the movie ONCE, and not each frame. This value never changes anyway, so why keep updating it? This may not look very useful, but apply this to big scripts, and it can really get the scripts running faster. You can also apply this same technique with loops. Variable Scopes Variables have a thing called the scope. The scope is where the variable can be accesed from. You have variables that only can be accesed from the function where they belong to, variables that can be accessed throughout the whole MovieClip and variables that can be accessed throughout the whole movie. Here are the ways of defining different variables with different scopes:var var1 = "value"; //first type var2 = "value"; //second type _global.var3 = "value"; //third type After defining the variables, you don't need to repeat the var or _global. You can just type the name. Functions & Classes Using a lot of functions can make the performance decrease. Each time a function is executed, Flash saves all the variables etc, and then executes the function. When the function is done, it takes back all the variables and continues the original script. This can sometimes really take some time, specialy when your using the code in a loop. Sometimes, using function is better because it increases the readibilty and useability of the script. If you want to make scripts accesible for other users, using functions is a must.It's basicly also the same story for classes. If you want it to be accessible for other users, use functions & classes.Tips You can go on and on testing your scripts, trying to find the fastest way to do it. But you won't seen any difference in just one execution. So if you want to test a scripts efficiency, put it in a loop of like 10,000 times. Then trace getTimer() minus the timer at the beginning of the test. Here's a little script: startTimer = getTimer(); testvar = 0; for (var t=0; t!=10000; t++) { testvar++; } trace(getTimer()-startTimer); Good luck! Author: MicrOchip
|
|
Premium Partners | |||||
|
|||||
Free website templates and paid web templates are great tools to make your websites look perfect! You will save time and money with our flash templates and free website templates
Our visitors are satisfied with the quality of our free and paid website templates! Please visit our free website templates and paid website templates sections. We offer free web templates, free web layouts, free web page templates and other stuff for free download. All templates come with the html and external css file so you may easily edit HTML with your favorite HTML editor. Feel free to download our free web templates for your personal websites. Terms of use depend upon the website template vendor. |
Home | Submit Tutorial | Top Sites | Free Templates | Website templates | Privacy Policy | Contact Us |