A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [AS2] Tile Based Games + hitTest

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    13

    [AS2] Tile Based Games + hitTest

    Alright I've been looking everywhere for this but I just cant seem to find it, while the original way of doing it, dont understand it well.

    Using Tonypa's Tutorial on Tile Based Games [which is quite outdated], Ive created a map with 2 tiles. Tile0 is walkable, Tile1 is not. Sounds simple.

    Code:
    var myMap = new Array();
    myMap[0] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
    myMap[1] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
    myMap[2] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
    myMap[3] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
    myMap[4] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
    myMap[5] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
    myMap[6] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
    myMap[7] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
    game = {tileW:50, tileH:50};
    game.Tile0 = function() {
    };
    game.Tile0.prototype.walkable = true;
    game.Tile0.prototype.frame = 1;
    game.Tile1 = function() {
    };
    game.Tile1.prototype.walkable = false;
    game.Tile1.prototype.frame = 2;
    function buildMap(map) {
    	_root.attachMovie("empty", "tiles", ++d);
    	game.clip = _root.tiles;
    	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]]();
    			game.clip.attachMovie("tile", name, game.clip.getNextHighestDepth());
    			game.clip[name]._x = (j*game.tileW);
    			game.clip[name]._y = (i*game.tileH);
    			game.clip[name].gotoAndStop(game[name].frame);
    		}
    	}
    }
    buildMap(myMap);
    Then I created a character and I plugged him onto the map and put some actionscript on him for moving around [contrary to putting actionscript on the frame so that the character can show up], which to me was easier. And now I go to try to make Tile2 'unwalkable', but first decided to test it to find the code.

    By moving my character in any direction, I wrote down trace("Hi") in a if (this.hitTest(__)) form. I pretty much put it every word I could find in there to figure out how it could trace "Hi", the closest I got was _root.tiles, but it meant that both the walkable and unwalkable tiles got hitTest.

    Is there any possible way from that code I can just hitTest one of the tiles. It would be a huge help.
    Last edited by 3Diablo7; 08-19-2010 at 04:43 PM.

  2. #2
    Senior Member
    Join Date
    May 2002
    Posts
    1,017
    What do you need hitTest for? Link the position of the character to that of the tiles. I.e if its position is [6,1] he won't be able to go left or down but up or right.
    while (2+2 == 4) {
    drink ("bourbon");
    }
    gotoAndStop("bed");

  3. #3
    Junior Member
    Join Date
    Nov 2008
    Posts
    13
    I need hitTest for the unwalkable tiles, which in this case is Tile1..But I cant seem to find a name for Tile1 so I can put it in the hitTest.

  4. #4
    Senior Member
    Join Date
    May 2002
    Posts
    1,017
    if each tile stores its coordinates and if you keep track of the character position you need to compare those against each other.
    while (2+2 == 4) {
    drink ("bourbon");
    }
    gotoAndStop("bed");

  5. #5
    Junior Member
    Join Date
    Nov 2008
    Posts
    13
    Not sure what your saying, but I believe that if I get the coordinates for each tile, I would have to have a really long code, while im just looking for something like this:
    [in the character's A.S.]

    if (Key.isDown(Key.RIGHT)) {
    this._x += 5
    if (this.hitTest('Tile1')) {
    this._x -= 5
    }
    }

    But I have no idea where I can get the name of Tile1 from the code in the first post.

  6. #6
    Senior Member
    Join Date
    May 2002
    Posts
    1,017
    then you don't need tiles. You can draw a plane, put some movieclips on top and check for hittest or for coordinates. Tiles you'd like to use if you have a pathfinding algorithm which calculates your path from tile A to tile B avoiding those that are not walkable etc.
    while (2+2 == 4) {
    drink ("bourbon");
    }
    gotoAndStop("bed");

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