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

Recommended Hosting:
Host Unlimited Domains on 1 Account
1500GB storage and 15000GB bandwidth for $6.95/mo!
Recommended Hosting:
Host Unlimited Domains on 1 Account
1500GB storage and 15000GB bandwidth for $6.95/mo!

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.
Anyway, getting back to efficiency. Using the first type, is allways the fastest, as the variables get deleted when your done with it. They can't be accesed from outside that current level at that time, so if you want to keep it for use in functions/events (like onEnterFrame), you need to use the second type. You can, however, acces this variable from outside the movieclip if you put the path in front of the variable. The slowest is _global. You should also avoid using this, as it increases the chance of accidently using the same variable name (and that allways causes loads of havoc).

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
Advertisements

Premium Partners

  Free Website Templates - Flash templates, Affordable Website Design, Website Templates, Website Redesign, Custom Website Design, Web Design Tutorials, Flash Tutorials, Promotion Tutorials - that is what we do. Metamorphosis Design Studio offers quality, free and low cost web site templates for your business and personal life, we also offer affordable web design and site re-design.
  Vertex Website Tempales - It saves tones of time and money to use pre-made web designs to build your web site. You need a web site but you don't want to pay thousands dollars to professional web design companies? Our web templates is just for you! They are designed to be easilly edited by your favorite html editor, like MS FrontPage
  Website Design Tutorials - Web site design is an area that simply takes experience to perfect. Get started with these tutorials covering everything from page layout to content tips. At Webdesigntutorials.net you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced Adobe Photoshop, Macromedia Flash MX and 3D Studio Max software.
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

All Right Reserved by ProDesignHost.com