Groups | Blog | Home
all groups > flash actionscript > july 2005 >

flash actionscript : Brackets


Jeckyl
7/3/2005 12:00:00 AM
[quoted text, click to view]
the () means 'call a function and pass the values in the () to the function'
... its a function call

[quoted text, click to view]
the [] means 'look in the object for a variable whose name is the same as
the value in the []' .. its a variable lookup

Jeckyl

azurepenguin
7/3/2005 12:00:00 AM
i think you only use [] in reference to an array? otherwise, i use () for a
condition and {} for the effect, as in
on (condition) {
action;
}
and that usually works. im pretty basic with action script too, sorry if this
is a) uesless or b) wrong.
cheers,
-sam
davidGlasgow
7/3/2005 12:00:00 AM
David Stiller
7/3/2005 11:10:51 AM
davidGlasgow,

[quoted text, click to view]

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/

davidGlasgow
7/3/2005 11:15:00 AM
Hi
I have been using action script at a very basic level for years. However I am
trying to improve my knowlege. One of the things that confuses me is when to
use what type of bracket. When ever you read a tutorial or book, however basic
is just tells you what brackect to use without any explaination.

Here is an example of my confusion

var p = random(ivar);
var t = this[ivar];

why the 2 types of brakects and whats the difference

Dose any one know of any good links or stuff to help expaline brackets

Cheers
AddThis Social Bookmark Button