7 Replies Latest reply on Jul 25, 2009 11:59 AM by christoon_ca

array loop problem

Hi

This is probably easily remidied, but my mind is a little over stuffed right now.

How do I make this work?

```for (i=0; i<connVis.length; i++)
if (connVis[i] == 1) {
wireConnVar[this]._x += 130
} else if (connVis[i] == 2) {
wireConnVar[this]._x -= 130
}
```

Any ideas are greatly appreciated.

Thanks

• 1. Re: array loop problem

I'm not sure what you want to do. Can you explian it with words, or put the whole code?

• 2. Re: array loop problem

that would probably be a good idea...

I want to find any variables in the connVis array that are equal to 1 or 2, and move their respective mc instances accordingly.

so if:

connVis[7] == 1

then move

wireConnVar[7] this amount

and if:

connVis[9] == 2

then move

wireConnVar[9] this other amount

Does that make sense?

Thanks

• 3. Re: array loop problem

That means connVis is an array, and wireConnVar is an array of objects. You can change the [this]s into [i]:

``for (i=0; i<connVis.length; i++){     if (connVis[i] == 1) {          wireConnVar[i]._x += 130     } else if (connVis[i] == 2) {          wireConnVar[i]._x -= 130     }}``

I think adding the brackets are better, but I think they will work without them.

• 4. Re: array loop problem

A couple of things, first: neither work for me. And secondly, when I looked at my email notification of your reply it read differently than it does on the forum page.

did you mean for it to read like this (note the asterisks):

for (i=0; i<connVis.length; i++){
if (connVis[i] == 1) {
wireConnVar[*i*]._x += 130
} else if (connVis[i] == 2) {
wireConnVar[*i*]._x -= 130
}
}

or this:

``for (i=0; i<connVis.length; i++){     if (connVis[i] == 1) {          wireConnVar[i]._x += 130     } else if (connVis[i] == 2) {          wireConnVar[i]._x -= 130     }}``

Also, I forgot to add that this is happening on a button event...

```myButton.onRelease = function() {
for (i=0; i<connVis.length; i++){
if (connVis[i] == 1) {
wireConnVar[i]._x += 130
} else if (connVis[i] == 2) {
wireConnVar[i]._x -= 130
}
}
};```

Thanks

• 5. Re: array loop problem

It doesn't matter if it is a button or not. This is the correct one:

``myButton.onRelease = function() {     for (i=0; i<connVis.length; i++){          if (connVis[i] == 1) {               wireConnVar[i]._x += 130          } else if (connVis[i] == 2) {               wireConnVar[i]._x -= 130          }     }};``

The brackets for "for" is optional, as long as it just contain one thing ( e.g. if/while/for statements, one line code etc. )

• 6. Re: array loop problem

It seems that tabs in quotes are longer than normal ones.

• 7. Re: array loop problem

So, on coming back to this I realized that:

1. You were right dalcde, the syntax was fine
2. I should post the whole code because then dalcde would have noticed my error immediately, which was that I was writing my array indices with names not their native numbers.

ie:

myArr = Array();

myArr[someName] = 0;

should be:

myArr = Array();

myArr[0] = 0;

Thanks