A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: randomly generated simple terrain

  1. #1
    Senior Member
    Join Date
    Feb 2003
    Posts
    146

    randomly generated simple terrain

    I'm trying to make a tile-based strategy game. I've never done tile-based before, so I'm kinda lost.

    Here's what I have:

    Code:
    map = new Array(10,10);
    
    
    tileW = 40;
    tileH = 40;
    function buildMap (map1) {
    	var mapWidth = map1[0].length;
    	var mapHeight = map1.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(map1[i][j]+1);
    		}
    	}
    }
    buildMap(map);
    as it is, it doesn't work because I can't seem to figure out how to randomly generate a 10x10 terrain. I know it has something to do with for-loops, but i don't know how to put a random number(0 or 1) into the map array.

    any help is appreciated

    thanks

  2. #2
    w00t PrED32's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    529
    instead of 'map = new Array(10,10)' use:

    map = randomArray(10,10,<NUMBER OF TILES YOU HAVE>);

    and then above that place:

    function randomArray(w,h,num){
    var arr = new Array();
    for(var y = 0;y < h;y++){
    arr[y] = new Array();
    for(var x = 0;x < w;x++){
    arr[y][x] = random(num);
    }
    }
    return arr;
    }

    and you'll have your random map.

  3. #3
    Senior Member
    Join Date
    Feb 2003
    Posts
    146
    thank you very much!

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