Moving things inside a two-dimensional array in as3?

81 Views Asked by At

I'm trying to create a clone of 2048 for a cs class in as3, and I'm having trouble with moving blocks around. I basically declare a 4x4 array:

var tiles:Array = new Array(new Array(4), new Array(4), new Array(4), new Array(4));

I set it all to empty string at the beginning:

for(i = 0; i < 4; i++){
    for(j = 0; j < 4; j++){
        tiles[i][j] = '';
    }
}

I also have a display function in order to display the array on the frame:

function display():void{
    for(i = 0; i < 4; i++){
        for(j = 0; j < 4; j++){
            if(tiles[i][j] != ''){
                tiles[i][j].x = 50 + (107 * j)
                tiles[i][j].y = 50 + (107 * i)
            }
        }
    }
}

I also have a keydown event to move the tiles:

for(i = 0; i < 4; i++){
    for(j = 0; j < 4; j++){
        if(tiles[i][j] != ''){
            switch (event.keyCode){
                case Keyboard.LEFT:
                    tiles[i][0] = tiles[i][j]
                    break;
                case Keyboard.RIGHT:
                    tiles[i][3] = tiles[i][j]
                    break;
                case Keyboard.UP:
                    tiles[0][j] = tiles[i][j]
                    break;
                case Keyboard.DOWN:
                    tiles[3][j] = tiles[i][j]
                    break;
            }
            tiles[i][j] = ''
        }
    }
}

It's supposed to set a new tile to the equivalent of the old tile, then set the old tile to '', but for some reason it sets both the new tile and the old tile to ''! I'm not sure what is going on here, can someone point me in the right direction?

0

There are 0 best solutions below