# Can someone explain this script?

• Newbie
• Posts: 11

3+ Months Ago

Hi. im having trouble understanding a script. its more on game programming and the script is about the creation of tiles. here goes the script..

Code: [ Select ]
myMap = [            //this part is the map matrix right? ok..
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1]
];
game={tileW:30, tileH:30}; // i dont know what this line do.
//can someone explain?
game.Tile0= function () {};    //declare class?
game.Tile0.prototype.walkable=true; //property for new class
game.Tile0.prototype.frame=1;     //property for new class right?
game.Tile1= function () {};        //same as above
game.Tile1.prototype.walkable=false;
game.Tile1.prototype.frame=2;

function buildMap (map) {               //buildmap function
_root.attachMovie("empty", "tiles", ++d);  //attachmovie to empty
game.clip=_root.tiles;    //what this line do?
var mapWidth = map[0].length;
var mapHeight = map.length;
for (var i = 0; i < mapHeight; ++i) {
for (var j = 0; j < mapWidth; ++j) {
var name = "t_"+i+"_"+j;
game[name]= new game["Tile"+map[i][j]];  //this also i do not know
game.clip.attachMovie("tile", name, i*100+j*2);
game.clip[name]._x = (j*game.tileW);   //what this line do?
game.clip[name]._y = (i*game.tileH);    //what this line do?
game.clip[name].gotoAndStop(game[name].frame);
}
}
}
1. myMap = [            //this part is the map matrix right? ok..
2. [1, 1, 1, 1, 1, 1, 1, 1],
3. [1, 0, 0, 0, 0, 0, 0, 1],
4. [1, 0, 1, 0, 0, 0, 0, 1],
5. [1, 0, 0, 0, 0, 1, 0, 1],
6. [1, 0, 0, 0, 0, 0, 0, 1],
7. [1, 1, 1, 1, 1, 1, 1, 1]
8. ];
9. game={tileW:30, tileH:30}; // i dont know what this line do.
10.                       //can someone explain?
11. game.Tile0= function () {};    //declare class?
12. game.Tile0.prototype.walkable=true; //property for new class
13. game.Tile0.prototype.frame=1;     //property for new class right?
14. game.Tile1= function () {};        //same as above
15. game.Tile1.prototype.walkable=false;
16. game.Tile1.prototype.frame=2;
17. function buildMap (map) {               //buildmap function
18. _root.attachMovie("empty", "tiles", ++d);  //attachmovie to empty
19. game.clip=_root.tiles;    //what this line do?
20. var mapWidth = map[0].length;
21. var mapHeight = map.length;
22. for (var i = 0; i < mapHeight; ++i) {
23.  for (var j = 0; j < mapWidth; ++j) {
24.   var name = "t_"+i+"_"+j;
25.   game[name]= new game["Tile"+map[i][j]];  //this also i do not know
26.   game.clip.attachMovie("tile", name, i*100+j*2);
27.   game.clip[name]._x = (j*game.tileW);   //what this line do?
28.   game.clip[name]._y = (i*game.tileH);    //what this line do?
29.   game.clip[name].gotoAndStop(game[name].frame);
30.  }
31. }
32. }

im sorry if that was long. i really took the effort on searching for those but the syntax game.clip[name] i donot know what is going on. and all the lines with what this line do... can someone help me understand this? thanks...
• Banned
• Posts: 143
• Loc: Ukraine

3+ Months Ago

Look arrays up in the actionscript disctionary or online.
• Genius
• Posts: 13506
• Loc: Florida

3+ Months Ago

Code: [ Select ]
myMap = [            //this part is the map matrix right? ok..
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1]
];
1. myMap = [            //this part is the map matrix right? ok..
2. [1, 1, 1, 1, 1, 1, 1, 1],
3. [1, 0, 0, 0, 0, 0, 0, 1],
4. [1, 0, 1, 0, 0, 0, 0, 1],
5. [1, 0, 0, 0, 0, 1, 0, 1],
6. [1, 0, 0, 0, 0, 0, 0, 1],
7. [1, 1, 1, 1, 1, 1, 1, 1]
8. ];

This is a multi-dimensional array, it will later tell the game weather or not each tile is walkable.

Code: [ Select ]
game={tileW:30, tileH:30}; // i dont know what this line do.
//can someone explain?
1. game={tileW:30, tileH:30}; // i dont know what this line do.
2.                       //can someone explain?

This is "shorthand" for,
game = new Object();
game.timeW = 30;
game.tileH = 30;

Code: [ Select ]
game.Tile0= function () {};    //declare class?

This is creating a class local to the game object.

Code: [ Select ]
game.Tile0.prototype.walkable=true; //property for new class
yep, inhertiable when a "new Tile0" is created.

Code: [ Select ]
game.Tile0.prototype.frame=1;     //property for new class right?

yep, same as above.

Code: [ Select ]
game.Tile1= function () {};        //same as above
game.Tile1.prototype.walkable=false;
game.Tile1.prototype.frame=2;
1. game.Tile1= function () {};        //same as above
2. game.Tile1.prototype.walkable=false;
3. game.Tile1.prototype.frame=2;

yep.

Code: [ Select ]
function buildMap (map) {               //buildmap function

function declaration, takes the multi-dimensional "myMap" array (or any multi dimensional array for that matter) as a paramater.

Code: [ Select ]
_root.attachMovie("empty", "tiles", ++d);  //attachmovie to empty

Other way around,
This attaches a movieclip that has been given a linkage identifier of "empty" to _root. It gives this newly attached MC an instance name of "tiles" & increments a "d" variable before setting the depth of the newly attached MC to the newly incremented value of "d"

Code: [ Select ]
game.clip=_root.tiles;    //what this line do?

This is the game object getting a reference to that newly attached movieclip named "tiles". It's setting up a container for attaching tile movieclips to later on.

Code: [ Select ]
var mapWidth = map[0].length;
var mapHeight = map.length;
for (var i = 0; i < mapHeight; ++i) {
for (var j = 0; j < mapWidth; ++j) {
var name = "t_"+i+"_"+j;
1. var mapWidth = map[0].length;
2. var mapHeight = map.length;
3. for (var i = 0; i < mapHeight; ++i) {
4.  for (var j = 0; j < mapWidth; ++j) {
5.   var name = "t_"+i+"_"+j;

This begins looping through that multi-dimensional array & creating a tile name for each element in that array. IE: t_0_0, t_0_1, ect..

Code: [ Select ]
game[name]= new game["Tile"+map[i][j]];  //this also i do not know

This took me a second to understand, I'll explain it as best as I can
basically it is using the generic "Tile0" & "Tile1" class constructors defined before to create the properties for each tile, Notice
new game["Tile + either 0 or 1 from that multi-dimensional array] to define if this tile will be walkable or not. The beginning "game[name] =" is simply creating game.t_0_0, since you can't do game."t_"+ map[i][j] = (at least I don't think you can...) & game.name would reference a name property of game instead of "game.t_0_1" so you do it associatively like this.

Code: [ Select ]
game.clip.attachMovie("tile", name, i*100+j*2);

Remember that container created to attach tiles to earlier ?
This is attaching a "tile" MC, "name" still uses the "t_0_1" convention for naming the newly attached MC. the iteraters i & j are used to calculate unique depths for each newly attached tile MC.

Code: [ Select ]
game.clip[name]._x = (j*game.tileW);   //what this line do?
game.clip[name]._y = (i*game.tileH);    //what this line do?
1.   game.clip[name]._x = (j*game.tileW);   //what this line do?
2.   game.clip[name]._y = (i*game.tileH);    //what this line do?

This is setting the X & Y positions of the newly attached tile MC. Once again it is referened associatively.

Code: [ Select ]
game.clip[name].gotoAndStop(game[name].frame);
}
}
}
1.   game.clip[name].gotoAndStop(game[name].frame);
2.  }
3. }
4. }

This is telling the newly attached tile MC to gotoAndStop at a certain frame.
Remember the "Tile0.prototype.frame=1" above ? this was inherited when the associatively created tile MC was created.
The first iteration of the loop would be using the top-left number in that multi-dimensional array which is a 1.
Now with that in mind this line would equate to,
game.clip.t_0_0.gotoAndStop(1);

Make sense ?

## Post Information

• Total Posts in this topic: 3 posts
• Users browsing this forum: No registered users and 8 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum