For loop

Hmmm. What’s wrong with this code?

for (r=1; r<4; r++) {
if (_level0.beml_side[r] == “Inferno”) {
indside[r] = 1;
} else if (_level0.beml_side[r] == “Storm”) {
indside[r] = 2;
} else if (_level0.beml_side[r] == " ") {
indside[r] = 3;
} else {
ind_side[r] = 0;
}
trace(indside[r]);
side[r].setSelectedIndex(indside[r]);
}
indside1, 2 and 3 all come up undefined. If I don’t use the for loop it works fine.

try this… (not sure… but it might give you better results)


for (r=1; r<4; r++) {
if (_level0.beml_side[r]._name == "Inferno") {
indside[r] = 1;
} else if (_level0.beml_side[r]._name == "Storm") {
indside[r] = 2;
} else if (_level0.beml_side[r]._name == " ") {
indside[r] = 3;
} else {
ind_side[r] = 0;
}
trace(indside[r]);
side[r].setSelectedIndex(indside[r]);
}

Now this really depends upon what it is you’re trying to test for. The _name property of a movie clip is the instance name that you assigned to the movie clip itself. If you’re looking to test for a url name then I’m not sure yet how to do that.

sorry… ignore that code… you’ll never get a correct response from that.

What is it that you are testing for in this if statement?

_level0 is the level of the movie that you’ve loaded into

beml_side[r] is the name of the movie clip inside that loaded swf

but what is “Inferno”? Where does that get set, is it an instance name?

Err. I don’t think it’s the name I’m trying to get.
_level0.beml_side1 = “Storm”
_level0.beml_side2 = “Storm”
_level0.beml_side3 = “Storm”

These values are all strings loaded up from a .txt file when the flash starts. I know it gets that far because I can check the values of these not only in the .txt file but also in the flash where it is aware of it too. If I replace the “[r]” with a number (say 2) it will execute the code correctly.

Edit: Before I had the for loop I had it in 3 separate if statements. One for each beml_side. It worked fine then. I then decided to add another value and condense the code into that for loop and it kinda went wonky. :slight_smile:

oh… and I think this is in error

{
indside[r] = 3;
} else {
ind_side[r] = 0;
}

shouldn’t this second “ind_side” be “indside”?

Ya my bad. That doesn’t effect it though. The values of indside1, indside2 and indside3 are all still undefined.

of course… variables… what the hell am I thinking. :stuck_out_tongue:

Well considering that… I’m not all that sure what’s wrong with the code except that second set variable statement being different than the first.

This might help you test to see what’s wrong.


for (r=1; r<4; r++) {
  trace(_level0.beml_side[r]);
}

at least you’ll have a full list of what each string variable is right off the bat. Let me know if they come up properly with the values you expected

Comes up all undefined.

I just traced:

_level0.beml_side1
_level0.beml_side2
_level0.beml_side3

As well. They gave me the right values.

Sorry… I think I found the problem.


for (r=1; r<4; r++) {
        if (_level0["beml_side"+r] == "Inferno") {
                indside[r] = 1;
        } else if (_level0["beml_side"+r] == "Storm") {
                indside[r] = 2;
        } else if (_level0["beml_side"+r] == " ") {
                _root["indside"+r] = 3;
        } else {
                _root["indside"+r] = 0;
        }
        trace(_root["indside"+r]);
        _root["side"+r].setSelectedIndex(_root["indside"+r]);
}

we use brackets when we want to add a number to the end of a string of letters like that.

Keep in mind that this all depends upon where those variables are supposed to be located. If the variables like “inside1” is locaed somewhere other than the _root timeline you’ll need to set the path there.

The important thing to note when using the brackets like this, in dot syntax is that a dot is NOT placed before the brackets but it is placed after them. So if we were setting the alpha property of a movie clip it would look liike this.

_root[“myMovieClip”+r]._alpha=0;

Ok that seemed to change the output but didn’t fix it… It’s now giving me the “else” outcome. I have a feeling it’s because the if statement is asking if the string “”_level0.beml_side"+r" is = to “Storm”. It’s not calculating the variable any further than _level0.beml_side1 for example.

Well yeah… it’s going to reach that final else statement and set each one to that IF it doens’t encounter a “true” before that point in time.

in addition, I still managed to screw up your script.


for (r=1; r<4; r++) {
        if (_level0["beml_side"+r] == "Inferno") {
                _root["indside"+r] = 1;
        } else if (_level0["beml_side"+r] == "Storm") {
                _root["indside"+r] = 2;
        } else if (_level0["beml_side"+r] == " ") {
                _root["indside"+r] = 3;
        } else {
                _root["indside"+r] = 0;
        }
        trace(_root["indside"+r]);
        _root["side"+r].setSelectedIndex(_root["indside"+r]);
}


hmm… let me think on the delema… you keep working on it and post any additional quirks you may encounter… If we can’t solve it now, then you can send me the file at my email and I’ll look at it in the morning.

Ah got it! I’ll need to take a lesson in copy + paste. :slight_smile: I missed the final line of the code you edited.

_root[“side”+r].setSelectedIndex(_root[“indside”+r]);

I had:

_root["side"+r].setSelectedIndex("indside"+r);

Oops. :sigh: Anyway, much thx for fixing my problem so quickly.

:)… well I was editing the posts as you were reading them so I might have had it wrong on my first try. please forgive. :stuck_out_tongue:

Glad it’s working for you. Brackets are a pain to tell the truth… it took me a long time to get used to how they were used.

Well what confused me was the fact that I could have sworn I had a script somewhere that had a for loop using pieces of code like the following.
[AS]for (x=1; x<10; x++) {
variable[x] = y;
}[/AS]

Oh well. As long as it’s working now. :wink:

you might have had that. What it’s calling there is to an Array, not a variable. In the case of an array, there is a name, then an open bracket, then a number (or name of feild) then a close bracket.

like so

myArray[0] = the first object in the array.

so in the case of

for (x=1; x<10; x++) {
variable[x] = y;
}

The script is looping 9 times, and setting a number of slots in the array called “variable” (which is confusing. :stuck_out_tongue: cause we never name anything variable) equal to y.

I’ve come across something in C programming that I’ve started to use in my action scripting. I begin any created object, variable, or array with a lower case letter of the type. This is very helpful once you’re scripts get large and confusing. I wish other would do it too, but as of yet it hasn’t caught on.
some examples

aThisIsAnArray
sThisIsAStringVariable
oThisIsAnObject
fThisIsAFunction

with the lowercase letters first… I always know exactly what I’m looking at when I’m working my code. I hope that I can convince at least the people here to start using this format… but who knows… people don’t like change. :slight_smile:

You’re right. It was a array. :slight_smile: I like the idea of appending that letter to the start of an object, variable etc. to describe what it is so just for you I’ll start doing it… :slight_smile: even if I don’t display my code frequently I’ll probably benefit from it a lot. :wink: