AS3 : How to use variable in "for loop" as movieclip name ? - actionscript-3

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;
}

Related

How To Create An Array of Movie Clip with number

I have multi movieclips in my stage and they have instanceName e.g. k1,k2,... . I want to createar=[k1,k2,k3,k4, ...].
var i: int;
var ar: Array = new Array();
for (i = 1; i < 5; i++)
{
ar[i-1] = ["k" + i];
}
trace(ar);
ar[1].x = 100;
But the end of code does not perform.
What you want to do is to create an array of MovieClips, but instead of it you create an array of Arrays of Strings. To achieve your goal, you need to find an instance of a clip on stage by its name. Here is how you can try to do that:
const array:Array = new Array();
for(var i:int = 0; i < 5; i++) {
const childName:String = "k" + (i + 1);
const myMovieClip:MovieClip = stage.getChildByName(childName) as MovieClip;
array.push(myMovieClip);
}

Is there any difference in performance between these two for loop?

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.

Creating variables in a systematical way using loops in AS3?

Is there? Like Let's say I need 5 variables in ActionScript 3.0 and would like to name them as follows:
var myVar_1 = "things";
var myVar_2 = "things";
var myVar_3 = "things";
var myVar_4 = "things";
var myVar_5 = "things";
But instead of having to type them 1 by 1, would it work in a loop? I can't seem to make it work and would really love some help/advice on this matter.
Yes, you can create a dynamic property name using [ ] array access:
var variables:Object = {};
for(var i:int = 0; i < 5; i++){
variables["myVar" + i] = "value " + i;
}
trace(variables.myVar3); // "value 3"
The variables object in this case could be replaced by any dynamic object, including MovieClips.
However, in most cases to store data by index it usually makes more sense to use an array. Example:
var variables:Array = [];
for(var i:int = 0; i < 5; i++){
variables.push("value " + i);
}
trace(variables[3]); // "value 3"
You should use Vector.<String>, Array, Object or Dictionary for that:
var variables:Vector.<String> = new <String>[];
for(var i:int = 0; i<5; i++)
{
variables[i] = "things";
}

What does 'for' do in Action Script 3?

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.

Adding indexes in array

I am adding two matrices (or possibly many) in ActionScript 3.0.
Now my problem is how can I add indexes in array that is something like this?
array1[1,2,3,4] + array2[2,4,5,6] = answer[3,6,8,10]
This function adds up all the arrays that are passed to it:
function sumOfArrays(...args):Array
{
var sum:Array = [];
var arrays:Array = [];
var longestArrayLength:uint = 0;
for (var i:int = 0, n:int = args.length; i < n; i++)
{
if (args[i] is Array)
{
arrays.push(args[i]);
longestArrayLength = args[i].length > longestArrayLength ? args[i].length : longestArrayLength;
}
}
for (var j:int = 0; j < longestArrayLength; j++)
{
sum[j] = 0;
for (i = 0; i < n; i++)
{
sum[j] += isNaN(arrays[i][j]) ? 0 : arrays[i][j];
}
}
return sum;
}
It can be used like this:
var sum:Array = sumOfArrays(array1, array2);
That's not possible. Arrays only allows access via one index. You'd have to write a method on your own for this. But be aware of the fact, that null would be referenced on answer at, 0, 1, 2, 4, 5 and so on.