Warning: Duplicate variable definition - actionscript-3

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.

Related

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.

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

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

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.

reading out multi dimensional array

the multi array is read in correctly
but reading out the array he messes up.
the action that suppose to be done on each element from array in array, he doesn't do it anymore when more indexes in 2 array are made.
when a second index in 2 array i made he doesn't do this action anymore on previous index, only the last made index.
thanks
private var Enemy:Array = new Array();//1st array
private var EnemyHull:Array = new Array();
private var waves:Array = new Array();//2 array ==> array 1 get in this
private function enterFrame(e:Event):void
{
//Enemy Ai
for(var i2:uint; i2 < waves.length; i2++){
for(var i:uint; i < Enemy.length; i++){
waves[i2][i].x -= 1;//when a second index in 2 array i made he doesn't do this action anymore on previous index, only the last made index.
}
}
}
private function enemySpawnen(event:TimerEvent):void
{
for(var i:uint = 0; i < hoeveelheidEnemy;i++){
//
}
if(Enemy[i] != null){;
viewContainer.addChild(Enemy[i]);
//
}
}
waves[iwaves] = Enemy;
iwaves++;
}
function shoot(e:Event):void
{
//
try{
for(var i2:uint; i2 < waves.length; i2++){
for(var i:uint = Enemy.length-1; i >= 0;i--){
if(kogel.hitTestObject(waves[i2][i])){
//
}
}
}
}
}
catch(e:Error){
}
}
}
Your for loops misses an important thing to work properly : you have to initialize your iterator (i or i2) to a starting value.
You wrote :
for(var i2:uint; i2 < waves.length; i2++){
for(var i:uint; i < Enemy.length; i++){
waves[i2][i].x -= 1;
}
}
But what you need is :
for(var i2:uint = 0; i2 < waves.length; i2++){
for(var i:uint = 0; i < Enemy.length; i++){
waves[i2][i].x -= 1;
}
}
Dont forget the = 0 in your loop declaration !

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.