davidGlasgow,
[quoted text, click to view] > var p = random(ivar);
> var t = this[ivar];
>
> why the 2 types of brakects and whats the difference
The first kind, (), are actually parentheses. There are two ways
parentheses are used in ActionScript (if someone else thinks I've missed a
usage, please jump in!). The main use is a kind of "action trigger" to
execute a function or method. Take random(), for example, which you used
yourself.
What is random? In ActionScript, random is a function when it appears
by itself; it is a method when it appears after the Math class
(Math.random()). In both cases, it returns a random number, but it does so
differently when a function than a method. (See the entry for each in the
ActionScript Language Reference [F1 key] for specifics.) Since random is an
action word, it needs to be executed. This is accomplished via the
parentheses. Since random returns a number, you *get* your number by
executing the function/method. So doing this ...
var randomNumber = random;
.... wouldn't give you anything, but this ...
var randomNumber = random(5);
.... does.
Now, in your example, you used a variable ivar in those parentheses. In
the case of random, the function would give you a random number between zero
and one less than the value of ivar. If ivar was 10, you'd get a number
between zero and nine. In addition to triggering a function or method,
parentheses can be a mechanism for accepting parameters. In the case of
random, the function accepts one parameter (an integer), and between the
parentheses is where you put it. (The method Math.random() does not accept
parameters, but the parentheses are still necessary to trigger the method.)
In summary, then, triggering and accepting parameters are lumped into
the first usage. The second usage is to assign order of operations to math
expressions, just as it is done in math on paper.
e.g.
1 + 5 * 2 is not the same as (1 + 5) * 2
Brackets, on the other hand, are known to ActionScript as the array
access operator. Brackets can be used to access values in an array, such as
the following ...
var myArray = new Array("a", "b", "c");
trace(myArray[0]); // outputs "a"
trace(myArray[1]); // outputs "b"
trace(myArray[3]); // outputs "c"
When used with objects, such as instances of the Object class or the
MovieClip class, the array access operator allows an object's properties
with strings, rather than dot-notation paths.
Normally, you would access a movie clip whose instance name is myClip as
follows (assuming it's in the _root) ...
_root.myClip
.... and if this clip were nested inside another, called AuntSallyClip, the
full path would be like this ...
_root.AuntSallyClip.myClip
.... but the array access operator allows you to use a string instead:
_root["AuntSallyClip"].myClip
.... which means you can use variables to create your strings ...
var str = "SallyClip";
_root["Aunt" + str].myClip
In the context of what you posted ...
this[ivar];
.... the word "this" would refer to some container object: perhaps a movie
clip, perhaps the main timeline (aka _root). The value of ivar would be a
string used to represent the full path to some object.
Perhaps you have a variable named "noodle" in the main timeline.
Assuming "this" contextually refers to the main timeline, you could get the
value of noodle as follows:
this.noodle
.... again, in the context of "this" being in the _root, this would be the
equivalent of saying ...
_root.noodle
.... and if the value of ivar was, in fact, "noodle", then ...
_root[ivar]
.... would be the same as writing ...
_root.noodle
.... and the same as ...
this.noodle
.... and the same as ...
this[ivar]
Make sense?
David
stiller (at) quip (dot) net
Tackling the ActionScript Language Reference
http://www.quip.net/tutorials/