Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Action script loop problems... 2

Status
Not open for further replies.

BigBadDave

Programmer
May 31, 2001
1,069
EU
...Yo,
Can any one help :

I want to change this :

if (_root.thing == 1) {
}
if (_root.thing == 1) {
_root.thing[1].play ();
}
if (_root.thing == 2) {
_root.thing[2].play ();
}
if (_root.thing == 3) {
_root.thing[3].play ();
}
etc...


Into this :

for (x=0; x<10; x++) {
if (_root.thing == x)
_root.thing[x].play ();
}


But it dosn't work, any ideas?? Regards

Big Dave

davidbyng@hotmail.com


** If I am wrong I am sorry, but i'm only trying to help!! **

 
Oops, I didn't mean to put the brackets around the first if bit :

if (_root.thing == 0) {
_root.thing0.play ();
}
if (_root.thing == 1) {
_root.thing1.play ();
}
if (_root.thing == 2) {
_root.thing2.play ();
}
if (_root.thing == 3) {
_root.thing3.play ();
}
etc...
Regards

Big Dave

davidbyng@hotmail.com


** If I am wrong I am sorry, but i'm only trying to help!! **

 
Its ok, I figured it out, what I need to do was use a temp bit (foo) :

for (x=0; x<18; x++) {
foo = _root.snow add x;
if (myrandom2 == x) {
foo.play();
}
}
Regards

Big Dave

davidbyng@hotmail.com


** If I am wrong I am sorry, but i'm only trying to help!! **

 
Actually thats still not working either?? Regards

Big Dave

davidbyng@hotmail.com


** If I am wrong I am sorry, but i'm only trying to help!! **

 
What is thing? An array? A variable? A clip?

Regards,
wink4.gif
ldnewbie
 
a movie clip Regards

Big Dave

davidbyng@hotmail.com


** If I am wrong I am sorry, but i'm only trying to help!! **

 
Try it this way:

for (x=0; x<18; x++) {
foo = &quot;_root.snow&quot; add x;
if (myrandom2 == x) {
foo.play();
}
}

before concatenation did you try it with only _root.snow1.play();
or other ones...?


Have Fun...

Sharky99 >:):O>

 
You could also go with:

foo=_root[&quot;snow&quot;+x];

or

foo=eval(&quot;_root.snow&quot;+x);

It all works...
 
Just thought I'd post this answer (about eval) to a similar question by DT (I consider him as a guru!), on another forum.


Well, as far as I know, the only way to write a dynamic path reference (ie. using some variable to make the naming non-fixed) is by using an associative array.

The other alternative is totally evil and should be avoided, it looks like this:
eval(&quot;_root.pants&quot;+ someNumber);

What eval does is truly evil... it spawns a new copy of the compiler engine for every time you call it - self modifying code. Not good. In JavaScript, you can actually do things like write dynamically executing code, but in Flash you cannot (otherwise the whole aScript compiler would have to be included with the plugin, which is bad and big). Anyway.
eval - BAD. (-:

Bear in mind that in the wonderful object oriented world of Flash goodness, that all objects are essentially arranged in an associative array - that is, an array that uses name pointers instead of numbers. So you can have

myArray[0] = &quot;hello&quot;;
and
myArray.greeting1 = &quot;hello&quot;;
i = 1;
trace(myArray[&quot;greeting&quot;+ i]); // would return string &quot;hello&quot;

Both are acceptable ways of writing array values! Indeed, movieClips function exactly in this way. The dot operator is just a shorthand way of writing associative arrays! So

_root.myPath.toMy.movieClip
is the same as
_root[&quot;myPath&quot;][&quot;toMy&quot;][&quot;movieClip&quot;]

Difference to note: In Flash, the existence of an associative value in an array does not increment the length property. In the case of myArray above, the length would only be &quot;1&quot;, as &quot;greeting1&quot; does not contribute to the length property. Unlike PHP, for example.


Regards,
wink4.gif
ldnewbie
 
Cheers wangbar that works great!!

I knew it could be done just wasn't sure how. Regards

Big Dave

davidbyng@hotmail.com


** If I am wrong I am sorry, but i'm only trying to help!! **

 
One other thing on &quot;eval&quot;...

Point taken about how it works under the hood but it's actually faster than the array method. Which is probably because it's Flash 4 syntax which is generally faster than 5.

I prefer the array syntax because it's easier to read and debug and with any luck, since the array object will be native code in Flash 6, it should match eval for speed soon.
 
Well there's a book listed on Amazon about Flash 6 due for publication in April and Macromedia showed the Beta at FlashForward last month....
 
Got a similar problem, but want to access a level by using a reference number. All the help files and books only refer to _levelX and nowhere anything like _level(x)

loadMovieNum (startup_movie, 3);
x=_layer3.framesLoaded;

I want to change it to :-

mylayer = 3;
loadMovieNum (startup_movie, mylayer);
foo=eval(&quot;_layer&quot; + mylayer);
x= foo.framesLoaded;

This is so I can easily change the order I load movies, by changing one value and not every instance of _levelX.

Is this possible and what is the best way to do it, because I cannot find any way of accessing a level with just the number that I used in loadMovieNum.

I can also generate a function to work on any level specified. e.g. answer=func_is_movie_loaded(my_level);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top