Just as title implies, my question is, what does the 'for' piece of code do in ActionScript 3? I apologize if this seems to be a stupid question; I've very recently started coding.
It's a type of loop.
for (var i:int = 0; i < 10; i++)
{
// Do something.
}
This says:
1. Create an int called i and set it to 0.
2. Check to see if i < 10. If not, stop executing the for loop and move on.
3. Do something.
4. Add 1 to i.
5. Go back to #2.
for is used to create a loop. It can be a loop trough an array:
var array:Array = [1,2,3];
for(var i:int = 0; i < array.length; i++) {
// do something
}
Or it could be an object.
var object:Object = {};
for(var i:String in object) {
// do something
}
Or you could just have an loop like
for(var i:int = 0; i < 10; i++) {
// do something
}
A for loop through children on stage / Movieclip:
for(var i:int = 0; i < numChildren; i++){
// do something
}
So you can do many things with the for.
Related
As I said: Is there any difference (in performance) between these two for loop?
Between this:
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
var currentObject:DisplayObject = displayObjects_[i];
currentObject.width = newWidth;
}
and this:
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
displayObjects_[i].width = newWidth;
}
I tested them before. The result said the first one was faster but I don't know if I did it right.
I know it's not really an answer to your question, but if you're looking for fastest way to iterate through this array you should do:
for each(var currentObject:DisplayObject in displayObjects_) {
currentObject.width = newWidth;
}
I tried this out on a bare-bones project using SDK 4.6. This is the code.
public class Main extends Sprite
{
public function Main()
{
var displayObjects_:Array = [];
for (var i:int = 0; i < 1000000; i++)
{
displayObjects_.push(new Sprite());
}
var start:int = getTimer();
for (i = 0; i < displayObjects_.length; i++)
{
var currentObject:Sprite = displayObjects_[i];
currentObject.width = 100;
}
var end:int = getTimer()
trace(end, start, end - start);
start = getTimer();
for (i = 0; i < displayObjects_.length; i++)
{
displayObjects_[i].width = 100;
}
end = getTimer()
trace(end, start, end - start);
}
}
These are the results.
Done(0)
[Starting debug session with FDB]
16703 16250 453
17141 16703 438
Like I said, it would be very surprising to see any difference between the two. You'll probably see more improvements through using Vector instead of Array. Otherwise, this stuff is too mundane to fuss over.
As I know, Actionscript compiler automatically moves variable definitions to the begin of the function. So, this loop doesn't declare variable each time, first sample the same that:
var currentObject:DisplayObject;
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
currentObject = displayObjects_[i];
currentObject.width = newWidth;
}
So, I think the difference only in one additional variable declare and it willn't affects performance. But 3vilguy's example is better.
I have 100 movieclips in my stage named mc1,mc2,...,mc100 .
I want to set their visibility to "false" by a for loop like this:
for ( var i:Int=1;i<=100;i++)
{
mc+i.visible=false;
}
How can I do that?
You could try:
for (var i:int = 1; i < 101; i++)
{
this["mc"+i].visible=false;
}
This will work on both timeline and document class.
However, this is not very efficient. If you're going to be using this loop more than once, it's better to store references in an array and iterate over that, rather than use these lookups each time:
Use this at the very start of the application:
var objects:Array = [];
for (var i:int = 1; i < 101; i++)
{
objectsArray[i] = this["mc"+i];
}
Then, when you need to cycle later, use this loop:
for (i = 1; i < 101; i++)
{
var mc:MovieClip = objectsArray[i];
//Now, do what you need to this eg mc.visible = false;
}
So what I'm trying to do is go through each element of the array "maps" which contains 4 movieclips and look and the children within each of those movieclips to see which are of type "Block". However I'm getting a #2006 error and I'm not sure why, can anyone help please?
function findBlocks()
{
trace("findBlocks()");
for (i=0; maps.length; i++)
{
for (var j=0; maps[i].numChildren; j++)
{
var mc = maps[i].getChildAt(j);
if (mc is Block)
{
blocks.push(mc);
}
}
}
trace("blocks array: " + blocks);
}
Your for loop conditions are incorrect, try this :
for (var i=0; i < maps.length; i++){
for (var j=0; j < maps[i].numChildren; j++){
var mc = maps[i].getChildAt(j);
if (mc is Block){
blocks.push(mc);
}
}
}
You have to remember that arrays and the display list start at 0, so the index of the last element in your lists is length-1, and in the case of a display list numChildren-1
i < maps.length
and
j < maps[i].numChildren
are what solve the problem
I have 3 variables each named variable1, variable2, variable3.
I now have a for loop where i'd like to acces each variable one after the other.
for(var i:int = 1; i<4; i++){
variable[i] = //whatever
}
how can i talk to the variables one after the other like above? which isnt correct. but, ya..
try this:
for(var i:int = 1; i<4; i++){
this["variable"+i] = //whatever
}
If I have 2 for loops both declaring var i, then the second will produce a warning:
public function twoLoops(){
for (var i:int = 0; i < 100; i++) {
}
for (var i:int = 0; i < 100; i++) { // i is already declared once
}
}
I understand the reason this happens, it's explained in this answer, but is there a way around it (except for declaring i at the beginning of the method)?
It's simple - just don't declare it in the second loop:
public function twoLoops(){
for (var i:int = 0; i < 100; i++) {
}
for (i = 0; i < 100; i++) { // i is already declared once
}
}
This will work with no error - as your warning tells you, it's already defined, so you can use it again, setting it back to 0 to allow the loop to execute properly.
If you are so adamant in using the loop the way you are using, consider wrapping up in a function:
public function twoLoops() {
for (var i:int = 0; i < 10; i++) {
}
(function(){
for (var i:int = 0; i < 100; i++) { // i is already declared once
}
})();
}
Though it would not give any warning, I wonder what purpose would it really solve for you.