A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Tonypa's old AS2 tile based tutorials...

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    23

    Tonypa's old AS2 tile based tutorials...

    I've been reading Tonypa's old AS2 tile based tutorials and while I'm very slowly getting a grasp on them, there's a small bit, that no matter how much I reread the tutorial, I can't wrap my head around it and was hoping for some clarification on the forums here. His code for building the maps are as follows:
    Actionscript Code:
    // our map is 2-dimensional array
    myMap = [[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]];
    // declare game object that holds info
    game = {tileW:30, tileH:30};
    // walkable tile
    game.Tile0 = function () { };
    game.Tile0.prototype.walkable = true;
    game.Tile0.prototype.frame = 1;
    // wall tile
    game.Tile1 = function () { };
    game.Tile1.prototype.walkable = false;
    game.Tile1.prototype.frame = 2;
    // building the world
    function buildMap(map) {
        // attach empty mc to hold all the tiles and char
        _root.attachMovie("empty", "tiles", ++d);
        // declare clip in the game object
        game.clip = _root.tiles;
        // get map dimensions
        var mapWidth = map[0].length;
        var mapHeight = map.length;
        // loop to place tiles on stage
        for (var i = 0; i<mapHeight; ++i) {
            for (var j = 0; j<mapWidth; ++j) {
                // name of new tile
                var name = "t_"+i+"_"+j;
                // make new tile object in the game
                game[name] = new game["Tile"+map[i][j]]();
                // attach tile mc and place it
                game.clip.attachMovie("tile", name, i*100+j*2);
                game.clip[name]._x = (j*game.tileW);
                game.clip[name]._y = (i*game.tileH);
                // send tile mc to correct frame
                game.clip[name].gotoAndStop(game[name].frame);
            }
        }
    }
    // make the map
    buildMap(myMap);
    stop();

    Oddly, after alot of uneducated study, I'm getting a handle on it....except this line

    Actionscript Code:
    game[name] = new game["Tile"+map[i][j]]();

    I get that it's creating an object for each tile and referencing back to it via the "Tile"+map[i[[j] part, but I don't understand the 1st part: game [name].

    I see this syntax alot in Tonypa's tutorials, but I'm not sure what's going on there. [] are for arrays, correct? By putting game[name], is this line putting the new tile objects into a new array? I've looked in alot of array tutorials as well as object tutorials, but I have yet to see any syntax for:

    something [something] = something

    I imagine it's establishing a variable, though I usually see var something = something. I guess ultimately, it's the [] brackets BEFORE the = sign that is confusing. Sorry for the VERY simple question, but thank you for any help guys.

  2. #2
    When you know are. Son of Bryce's Avatar
    Join Date
    Aug 2002
    Location
    Los Angeles
    Posts
    838
    The brackets are for referring to a variable within an object by using a dynamic text string.

    What "game["Tile"+map[i][j]]" ends up being is "game["Tile"+1]" which Flash will interpret as a variable or object named "game.Tile1". Which in this case created a new tile based on that prototype Tile1(), so the code will be executed as...

    Actionscript Code:
    game[name] = new game.Tile1();

    I'm not completely familiar with how prototypes work in AS2, but I believe it's the equivalent to a "class", a bit of code you can execute anywhere or create multiple instances of.

    Another example for when you might use the brackets, say you have a bunch of enemies or bullets. You may loop through all of them on an enterframe to make them move. They're named "enemy1, enemy2, enemy3, etc." You might make a loop like this.

    Actionscript Code:
    var enemyTotal = 10;
    for (var i = 0; i < enemyTotal; i++) {
       trace("moving enemy" + (i + 1) );
       this["enemy" + (i + 1)].move();
    }

    Hope that clears your confusion!

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    23
    The brackets are for referring to a variable within an object by using a dynamic text string.
    A dynamic text string is simply a text string that is constantly changing, right? And that line is just referencing each "name" variable (IE: t_0_0, t_0_1, etc..) to the Tile1, Tile1, Tile0, functions etc....? Once that is established, it goes about placing each Tile1, Tile1, Tile0, etc..

  4. #4
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Basically you're targeting/creating something within the object's array of properties by name.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center