FORUMS: list search recent posts

Help me understand a syntaxe

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
evrard
Help me understand a syntaxe
on Apr 29, 2007 at 2:11:03 pm

Hello,

I am trying to read some professional codes but i always stumble on some syntaxes that send me back to actionscript school. Here is one case:

// Definition of an object

1- var game:Object = new Object();
2- game.Columns = 10;
3- game.rows = 10;
4- game.spacing = 30;
5- game.depth = 1000;
6- game.path = this.grid;
7- game.numberOfTypes = 8;

// Next, a function called buildGrid() is defined.

8- function buildgrid(){
9- for (var:j:Number = 0; j 10- for (var:i:Number = 0; i 11- var name:String = "cell"+i+"_"+j;
12- var x:Number = i*game.spacing;
13- var y:Number = j*game.spacing;
14- var type:Number = 1;
15 - game.path.attachMovie("cell", name, ++game.depth);
16- game.path[name]._x = x;
17- game.path[name]._y = y;
18- game[name] = {x:i, y:j, name:name, type:type,
19- clip:game.path[name]};
20- }
21- }
22- }

Ok, i understand the first first defined an object (line 1 thru 7), then he used the custom properties in there to store values that he used in the function buildGrid defined in line 8 thru 22.

My problem is the lines 18 and 19. In the book he explains this is an object that is created to store information about the "cell" created after each loop cycle. To be frank with you i dont understand this syntaxe at all. Quesions

a) Why did he use those "[]" instead of dot notation? Is game[name] same as if he typed game.name? when to use one notation when to use the other?

b) Because each new game[name] is named after the game object introduced in line1 i would expect them to hold values related to the variables in line 2 to 7, while here i see that they hold brand new properties different from the ones i do expect. What is the relation betwen these game[name] and the game object itself then? Could them been named otherwise?

Thank you very much for commenting these codes and answer my questions as it would help me greatly in my actionscripting learning. Once again thank you.

Evrard






Return to posts index

evrard
Re: Help me understand a syntaxe
on Apr 29, 2007 at 2:18:18 pm

Sorry for typo in the code. Please read the builGrid() function as follow:

// Next, a function called buildGrid() is defined.
8- function buildgrid(){
9- for (var j:Number = 0; j 10- for (var i:Number = 0; i 11- var name:String = "cell"+i+"_"+j;
12- var x:Number = i*game.spacing;
13- var y:Number = j*game.spacing;
14- var type:Number = 1;
15 - game.path.attachMovie("cell", name, ++game.depth);
16- game.path[name]._x = x;
17- game.path[name]._y = y;
18- game[name] = {x:i, y:j, name:name, type:type,
19- clip:game.path[name]};
20- }
21- }
22- }





Return to posts index

Sher Ali
Re: Help me understand a syntaxe
on Apr 29, 2007 at 3:16:40 pm

Its easy to understand that syntax and I am trying to make things clear for you here:

You see in the line the author is composing a string from other string(s) and variables on line 11 as

var name:String = "cell"+i+"_"+j;

The above line will hold reference to each new created movieclip created through the attachMovie method of the MovieClip class.

Let say we have two movieclips on the stage named mc1 and mc2 each having some tween animation for ten frames and we want to tell the movieclips to play the animation through actionscript, then in our actionscript we can reference to those movieclips in two ways.

First way:

_root.mc1.gotoAndPlay(1);
_root.mc2.gotoAndPlay(1);

Second and better/best way:

for(var i=1;i<=2;i++){
_root["mc"+i].gotoAndPlay(1);
}


for the first iteration of the above for loop the code segment will give us the reference for:

_root["mc"+i] will return _root.mc1


and for the second iteration of the above for loop the code segment will give us the reference for:

_root["mc"+i] will return _root.mc2


Let say now we have 100 or 1000 movieclips then referencing them using:
_root.mc1
_root.mc2
_root.mc3
.
.
.
_root.mc1000

will kill you or anybody


So we can use an easy approach:

for(var i=1;i<=1000;i++){
_root["mc"+i].gotoAndPlay(1);
}


Hopefully the explanations help.

------------------------------
Sher Ali
http://www.webeyestudio.com


Return to posts index


evrard
Re: Help me understand a syntaxe
on Apr 29, 2007 at 7:52:38 pm

Thank you Ali,

Your are indeed very helpful. You answered the first question very good, the second one actually remains unanswered (case of the object created within the function and its relation with the game object defined earlier).

"First way:

_root.mc1.gotoAndPlay(1);
_root.mc2.gotoAndPlay(1);

Second and better/best way:

for(var i=1;i<=2;i++){
_root["mc"+i].gotoAndPlay(1);
}
"

Could this also be equivalent?

for(var i=1;i<=2;i++){
_root.mc[i].gotoAndPlay(1);
}





Return to posts index

Sher Ali
Re: Help me understand a syntaxe
on Apr 30, 2007 at 4:13:51 am

I have answered that as well. No the following two cannot be equivalent:

for(var i=1;i<=2;i++){
_root["mc"+i].gotoAndPlay(1);
}

The above will give use _root.mc1 and _root.mc2, whereas

for(var i=1;i<=2;i++){
_root.mc[i].gotoAndPlay(1);
}

will give us _root.mc.1 and _root.mc.2

So both of the above code segments are not same and giving us different results.


-----------------------------
Sher Ali
http://www.webeyestudio.com


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]