dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [ Help ] Tile Based hitTest

  1. #1
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379

    [ Help ] Tile Based hitTest

    Hey I couldn't find this anywhere else specifically, but in my game I have used 2 for() loops to generate a map (400x400) thus 20x20 tiles. Now the outside edge and all the walls inside are one type of tile which cannot move and most of the others can only be moved in a specific direcitons (up tiles, left tiles, etc). The character will not walk through these walls as I've already done the check to reset his position if he were to move into a wall. However, any of the blocks that can be moved do not stop when pushed towards a wall, but instead travel into the wall.

    My first question is how I would compose a check to see if one tile -- _root["t_"+i+"_"+j] -- would collide with any other tile on screen.

    Secondly, how where would I put this code? I'm not sure but I can't seem to put code into an attached mc? See code below for more info.

    Code:
    //in the _root
    
    function buildMap(map) {
    	var mapWidth = map[0].length;
    	var mapHeight = map.length;
    	for (var i = 0; i<mapHeight; ++i) {
    		for (var j = 0; j<mapWidth; ++j) {
    			this.attachMovie("tile", "t_"+i+"_"+j, ++d);
    			this["t_"+i+"_"+j]._x = (j*tileW);
    			this["t_"+i+"_"+j]._y = (i*tileH);
    			this["t_"+i+"_"+j].gotoAndStop(map[i][j]+1);
    		}
    	}
    }
    ^This is the function that compiles the map.^

    Code:
    //in the user
    onClipEvent (enterFrame) {
         for (var i = 0; i<20; ++i) {
              for (var j = 0; j<20; ++j) {
                   if (this.hitTest(_root["t_"+i+"_"+j])) {
                        switch (_root["t_"+i+"_"+j]._currentframe) {
    		case 2 :
    			if (pushing == "true") {
    				if (box == "up") {
    					_root["t_"+i+"_"+j]._y -= speed;
    				} else if (box == "right") {
    					_root["t_"+i+"_"+j]._x += speed;
    				} else if (box == "down") {
    					_root["t_"+i+"_"+j]._y += speed;
    				} else if (box == "left") {
    					_root["t_"+i+"_"+j]._x -= speed;
    				} else {
    				}
    			}
    ^Here's is how I checked for one type of block that could be moved in any direction.^



    Any help is greatly appreciated as this is the only thing that stops me from finishing this game.

  2. #2
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    I am still stuck on this so if anyone could help it would be greatly appreciated.

  3. #3
    :
    Join Date
    Dec 2002
    Posts
    3,518

    Maybe try something like this...

    Code:
    if (pushing == "true") {
    	if (box == "up") {
    		if (_root["t_"+(i-1)+"_"+j]._currentframe != 1) {
    			_root["t_"+i+"_"+j]._y -= speed;
    		}
    	} else if (box == "right") {
    		if (_root["t_"+i+"_"+(j+1)]._currentframe != 1) {
    			_root["t_"+i+"_"+j]._x += speed;
    		}
    	} else if (box == "down") {
    		if (_root["t_"+(i+1)+"_"+j]._currentframe != 1) {
    			_root["t_"+i+"_"+j]._y += speed;
    		}
    	} else if (box == "left") {
    		if (_root["t_"+i+"_"+(j-1)]._currentframe != 1) {
    			_root["t_"+i+"_"+j]._x -= speed;
    		}
    	} else {
    	}
    }
    Last edited by dawsonk; 08-31-2006 at 06:47 PM.

  4. #4
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    That doesn't stop them from moving through other tiles though, which the part that's losing me.

  5. #5
    Senior Member TeroLebe's Avatar
    Join Date
    Mar 2003
    Location
    Lahti, Finland
    Posts
    302
    eh, If you're using tilebased map, You don't actually need to use "hitTest"-funktion.

    Check only tiles next to player, to see if the block is walkable.
    make a own funktion (like function isPassableTile(x,y) which returns a boolean value true or false).

    HitTest is kind of pixel based collision detection funktion, so it's too slowish for simple box shaped items.

  6. #6
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    Exactly what Tero said - hittest is never needed in tile based game.
    I have done several experiments with tiles that can be pushed and I will try to explain basic idea:
    *the stage is made up from 2 types of tiles, floor tiles and wall tiles
    *all the pushable blocks are created as separate objects
    *all the pushable blocks are larger then half of the tile size, this means there can be always only 1 center of pushable block on each tile
    *the blocks are named based on which tile the center of block is on, because only 1 center is over each tile all blocks have different names
    *now lets say main hero is walking on floor tile, it can not walk onto wall tile and this is checked by comparing positions of its 4 corner points after adding speed to current position of hero
    *beside checking for wall tiles we also need to check if hero will push a block
    *hero can only push a block if there is block on 3 tiles in his direction. Lets say hero moves right, he can push block one tile right (x+1) or right and above (x+1, y-1) or right and down (x+1, y+1)
    *if block is found, simple rectangle vs rectangle check is run to see if hero actually hits the block
    *if hero hits the block found another check is needed to determine if the block can move in same direction, the block can be pushed only if it does not hit wall tile and it does not hit another block
    *if block can be pushed in that direction, both hero and block are moved
    *if the center of block was moved into new tile, the name of block is changed

  7. #7
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,379
    Thanks a lot tony (after all, it was your tutorial I got the function from ). I'll post back in a few days when I get some time to play around with it. That's probably going to take me some time to digest though.
    Last edited by ImprisonedPride; 09-01-2006 at 12:48 PM.

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