Does anyone know a way to streamline this?
its area reset button for a whole bunch of MC (total of 70)
this is just a section.
so on release I want to send the MCs back to their origins. this does it, but its hell to code.
any effeciency advice?
==============================
It looks like you have all your MC's named in sequence, so you should be able to use a "for" loop to go through each MC and return them to their proper locations. But first you have to make sure they know where their starting position was in the first place. So when you have the frame with the "word" mcs appear, attach the following code:
Code:
for (i=0; i<70; i++) {
w = eval("word"+i);
w.xp = w._x;
w.yp = w._y;
}
This has each "word" MC store it's original x and y positions in the variables "xp" and "yp". Now that they know where they were before they moved, they can be told to go back to that spot using a for loop within a button event, like this:
Code:
backBtn.onPress = function() {
for (i=0; i<70; i++) {
w = eval("word"+i);
w._x = w.xp;
w._y = w.yp;
}
};
Last edited by Quixx; 06-01-2006 at 11:52 AM.
Reason: typo
It works!
You know, I spent a good hour hand coding that only to find out that if I ever re-sized the images, all would go to hell.
Which brings me to my next question. I'm going to do my own looking into, but would you happen to know of any good tutes or articles on "For loops"
they seem pretty important and a powerful tool to have.
I don't know of any tutorials for them myself (I never really looked). What I know of them, I've learned through the help box in Flash, the forums here, and a few books I browsed through at Barnes and Noble.
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