A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: I Want to make this more efficient

Threaded View

  1. #7
    All 1s and 0s dmonkey's Avatar
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Posts
    606
    Hi,

    They aren't too complicated - I could explain them in detail if you'd like. When you want to perform the same set of commands many times, you use loops. The types of loops we have at our disposal are the 'for', 'for ... in', 'do ... while' and 'while'. We're going to concentrate on the 'for' loop.

    This loop begins with a Number that we give it. We then give it a rule for what to do to that number each time we perform our set of commands. The rule will be something simple, like "add 1". We also need to provide a reason to stop. This is usually something like "our number has got too big" or "our number has got too small".

    The best way to see is to have a simple example. First off we create a new Number for the loop to use (for some reason, coders like to use the letters i, j, k... to denote these):

    code:

    var i:Number = 0;



    We are going to start i at zero, but we could start at any number as long as we form the rules correctly. Now we create the 'for' loop:

    code:

    for (i; i < 10; i++) {

    }



    This is how all 'for' loops are constructed. Here's what the arguments mean:

    for (the number i want to use ; when to stop ; how will the number change each time);

    In my case, the number I want to use is i. The rule to stop is formulated such that the loop will continue as long as this expression is true. In our case, the expression is i < 10. So as long as this remains true the loop will continue. This means that the loop will stop once i becomes greater than or equal to 10.

    The last part tells Flash what to do to the number on each run through of the loop. I want the number to increase by 1 each time, so I use the shorthand i++, which is the same as i = i + 1. I could have also put here i = i + 2, or i = i * 2 etc. Let's run the full loop with some commands inside:

    code:

    var i:Number = 0;

    for (i; i < 10; i++) {
    //Trace;
    trace("The current value of i is "+i);
    }



    In the output screen, we get this:

    The current value of i is 0
    The current value of i is 1
    The current value of i is 2
    The current value of i is 3
    The current value of i is 4
    The current value of i is 5
    The current value of i is 6
    The current value of i is 7
    The current value of i is 8
    The current value of i is 9

    As you can see, we started out with i = 0, as we specified. Then, Flash checks our rule for continuing. It finds that i < 10 (since i = 0), so we get the first output "The current value of i is 0". Then, flash applies the rule for what to do to i, namely increase it by 1. Again, flash finds that i < 10, so we get the next output. As you can see, after i = 9 we get to i = 10. At this point, we no longer satisfy the criteria for continuation, since 10 is not less than itself!

    Try putting i = i + 2 as the rule and take a look at the output. We now only get half as many outputs, because we miss out 1, 3, 5, ... . Now try i = i - 1. Oops! Flash crashes, and says that we are performing an infinite loop. This is because i starts at zero and keeps going more and more negative. No matter how many times we perform the rule, i is ALWAYS less than 10. So the loop never stops! This is something to look out for when writing these kinds of loops: the rules must be consistent!

    The main power of 'for' loops is that we can actually use the value of 'i' in our statements. This allows us to do what we did in your case and target different movie clips each time, even though our actual statements stay the same! Here's a simple math example:

    code:

    var i:Number = 0;

    for (i; i < 10; i++) {
    //Trace;
    trace(i*i);
    }



    This will give you all the so called "square" numbers between 0 and 100 (not including 100). Because we can make up our own rules and start on any number we want, you can see that we can really generate a vast array of different operations using the 'for' loop. We can also skip steps by manipulating i within the loop:

    code:

    var i:Number = 0;

    for (i; i < 10; i++) {
    //If i == 3, skip to 8;
    if (i == 3) {
    i = 8;
    }
    trace(i*i);
    }



    I hope this has given you a taste for the vast range of applications that loops have. I have really only given a few trite examples of what for loops are used for, but this is the most commonly found.

    One last thing you'll see around is that people don't often define the number outside the loop, as you can do it right inside like this:

    code:

    for (var i:Number = 0; i < 100; i = i*2) {



    This is just to save on typing. One last thing to watch out for is that you MUST MUST MUST use semicolons ( ; ) between the arguments. This tripped me up soooooooo many times when I was learning flash. So when you get errors about 'for' loops not working (and you will) always check for this first!

    If you have any more questions, let me know.

    Good luck!
    Last edited by dmonkey; 06-02-2006 at 07:19 AM.
    "If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center