Hello, I am currently working on a 2d isometric game. The engine uses a 3d array(tile[z][x][y] carrying the map data in which is looped over in order to build/check the map. Currently I am having some issues implementing height’s and slopes to my game world. As I try to move up a slope tile ,I am unable to find a good way to change the z coordinate for the player without crashing the engine. This is due for the function that finds the current tile coordinates is not checking the second layer of tiles.
I want to be able to move up a slope in the way that for example: If I am going up the slope the current coordinates are z=0,x=1,y=1 and I want to move to a tile next to the slope, I want the z coordinate to change to 1. If anyone has a idea of how I can solve this issue, I would appreciate it.
Here is the code that I think you will need
GetTile Function
//Get tile from coordinate
public function getTile(_x:Number, _y:Number, _z:Number):Tile {
var tile:Tile;
var mc:MovieClip = new MovieClip();
var global:Point = new Point(_x, _y);
global = localToGlobal(global);
//addChild(mc);
c: for (var i:int = 0; i < tiles[_z].length; i ++) {
r: for (var j:int = 0; j < tiles[_z]*.length; j ++) {
if (tiles[_z]*[j].hitTestPoint(global.x, global.y,true)) {
tile = tiles[_z]*[j];
//removeChild(mc);
//mc = null;
//global = null;
}
}
}
return tile;
}
Function that builds the map
private function createGame():void {
var temp = new Array();
//Build tiles ************************************************
temp[0]=[[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],
[2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2],
[2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2],
[2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2],
[2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2],
[2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2],
[2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2],
[2,1,1,1,1,2,2,2,2,1,3,1,1,1,1,1,1,1,1,1,1,1,1,2],
[2,1,1,1,2,2,2,2,2,1,3,1,1,1,1,1,1,1,1,1,1,1,1,2],
[2,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,2,2,2,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,2,2,2,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,2,2,2,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,2,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,0,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]];
var s:int = 0; //Height
var i:int = 0; //Row
var j:int = 0; //Column
var tile:Tile;
var current:Tile;
for(s = 0; s < temp.length;s++){
tiles.push([]);
for (i = 0; i < temp[s].length; i ++) {
tiles[s].push([]);
for (j = 0; j < temp[s]*.length; j ++) {
tile = new Tile(temp[s]*[j]);
if(s > 0){
tile.x = (52 / 2) * (i - j) + offSetX ;
tile.y = (26 / 2) * (i + j) + offSetY - 20 * s ;
tile.coordinates.z = s;
tile.coordinates.y = i;
tile.coordinates.x = j;
addChild(tile);
}
if(s == 0){
tile.x = (52 / 2) * (i - j) + offSetX ;
tile.y = (26 / 2) * (i + j) + offSetY ;
//tile.z = (52 / 2) * (i + j) + 100;
tile.coordinates.z = s;
tile.coordinates.y = i;
tile.coordinates.x = j;
//tile.alpha=0.5;
addChild(tile);
}
//Add to tiles array
tiles[s]*[j] = tile;
}
}
}
//Add tile objects
for(s = 0; s < temp.length;s++){
for (i = 0; i < tiles[s].length; i ++) {
for (j = 0; j < tiles[s]*.length; j ++) {
current = tiles[s]*[j];
if (current.obstacle) obstacles.push(current);
}
}
}
//trace ("Current map: " + tiles.length + " x " + tiles[0].length + " tiles");
//Add player
player = new Player(main, this, hud,getCoordinate(tiles[0][1][1],true,false,false),getCoordinate(tiles[0][1][1],false,true,false),getCoordinate(tiles[0][1][1],false,false,true),enemy);
//player.x = getCoordinate(tiles[1][1], true);
//player.y = getCoordinate(tiles[1][1], false);
camera = player;
//edd enemy
enemy = new Enemy(main,this,player,getCoordinate(tiles[0][2][2],true,false,false),getCoordinate(tiles[0][2][2],false,true,false));
//enemy.x = getCoordinate(tiles[1][1], true);
///enemy.y = getCoordinate(tiles[1][1], false);
//this.visible = false;
//this.y=290;
//this.x=10;
depthSort(player,enemy);
}
Collision Detection Function
private function checkWalls():void {
new_tiles[zPos] = board.tiles[zPos]; //Fill new_tiles object
var p:Tile = board.getTile(x, y,zPos);
trace("tile x"+p.coordinates.x);
trace("tile y"+p.coordinates.y);
trace("tile z"+p.coordinates.z);
trace("tile type"+p.type);
trace("zPos"+zPos);
var s:int = 0;
var i:int = 0;
var j:int = 0;
var lastX:Number;
var lastY:Number;
var lastTileX:Number;
var lastTileY:Number;
var tile:Tile;
var top:Boolean;
var bottom:Boolean;
var check:int = 4;
current.x = x;
current.y = y;
current = board.localToGlobal(current);
for (i = 0; i < board.tiles[zPos].length; i ++) {
for (j = 0; j < board.tiles[zPos]*.length; j ++) {
top = bottom = false;
tile = board.tiles[zPos]*[j];
if(tile.type == 1) {
if(p.coordinates.y == tile.coordinates.y){
if(p.coordinates.x == tile.coordinates.x){
Last.x = x;
Last.y = y;
}
}
}
if(tile.type==2){
if(p.coordinates.y == tile.coordinates.y){
if(p.coordinates.x == tile.coordinates.x){
x = Last.x;
y = Last.y;
vx = 0;
vy = 0;
}
}
}
if(tile.type==3){
if(p.coordinates.y == tile.coordinates.y){
if(p.coordinates.x == tile.coordinates.x){
// Last.x = x;
//Last.y = y;
//vx = 0;
//vy = 0;
}
}
}
}
}