AS3多维数组addChild

时间:2014-09-06 11:10:26

标签: arrays actionscript-3 multidimensional-array

我是阿姆斯特丹媒体学院的荷兰游戏发展学生

我的学校项目有点问题.. 我需要制作Boulder Dash 1984的精确副本,

我正在尝试使用Multidimesional数组制作关卡的地图。 但是如果我试图将它们添加到具有不同位置的舞台上,它将只添加1个对象......

有人可以帮我这个吗?

private var cellHeight:Number = 68;
private var cellWidth:Number = 68;

private var _map:Array;
private var _stone:Stone;
private var _wall:Wall;
private var _wall2:Wall2;
private var _diamond:Diamond;
private var _dirt:Dirt;
private var _player:Player;

public function Game(s:Stage){
  _player = new Player();
  _stone = new Stone();
  _wall = new Wall();
  _wall2 = new Wall2();
  _diamond = new Diamond();
  _dirt = new Dirt();

  _map = new Array(21);
  _map[0] =     [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row1
  _map[1] =     [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row2
  _map[2] =     [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row3
  _map[3] =     [_wall, _wall, _wall2, _player, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row4
  _map[4] =     [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row5
  _map[5] =     [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row6
  _map[6] =     [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row7
  _map[7] =     [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row8
  _map[8] =     [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row9
  _map[9] =     [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row10
  _map[10] =    [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row11
  _map[11] =    [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row12
  _map[12] =    [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row13
  _map[13] =    [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row14
  _map[14] =    [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row15
  _map[15] =    [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row16
  _map[16] =    [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row17
  _map[17] =    [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row18
  _map[18] =    [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row19
  _map[19] =    [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row20
  _map[20] =    [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row21
  _map[21] =    [_wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall, _wall]; //row22

  for ( var rows:int = 0; rows <= 21; rows++ )
  {
    for ( var column:int = 0; column <= 39; column++ )
    {
        addChild( _map[rows][column] );
        _map[rows][column].x = column * cellWidth + 34;
        _map[rows][column].y = rows * cellHeight - 34;
    } 
  }
}

所有_walls都只是占位符。但如果我跑这个,它只会给我一个墙和一个玩家......

问候,贾斯汀。

2 个答案:

答案 0 :(得分:1)

你最好定义一个计划,让我们说1代表wall1,2代表wall2 ......然后将它们放入数组......当它完成时你可以做一个代码它并在地图中添加了一个新墙,因为当您将相同的墙输入此阵列时(动作脚本只占用一个墙的一个实例,因为您没有创建新墙)

答案 1 :(得分:0)

除了给出的答案,提供一些创作策略。

private var cellHeight:Number = 68;
private var cellWidth:Number = 68;

private var _map:Array;
private var _stone:Stone;
private var _diamond:Diamond;
private var _dirt:Dirt;
private var _player:Player;
private const _rows:uint = 21;

public function Game(s:Stage){
  _player = new Player();
  _stone = new Stone();
  _diamond = new Diamond();
  _dirt = new Dirt();
  _map = [];

  for ( var row:int = 0; row <= _rows; row++ ) {
    for ( var column:int = 0; column <= 39; column++ ) {
      // create an instance for each cell on the grid.
      var element:DisplayObject = getElementFor(row, column);
      element.x = column * cellWidth + 34;
      element.y = rows * cellHeight - 34;
      _map[row][column] = addChild(element):
    } 
  }
}

function getElementFor(row:int, column:int):DisplayObject {
  if (row == 3 && column == 2) return new Wall2();
  if (row == 3 && column == 3) return _player;
  return new Wall();
}

如果要动态创建地图,请在“方法”中创建地图,例如:

private const WALL:uint = 1;
private const WALL2:uint = 2;
// could be read from a text file, use parseInt to match the data properly
private var _mapData:Array = [
    [1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1],
    [1, 1, 2, 1, 1, 1, 1]];

private function getMap(mapData:Array):Array {
  const map:Array = [];
  for (var rowIndex:uint = 0; rowIndex < mapData.length; rowIndex++) {
    var row:Array = mapData[rowIndex];
    map[rowIndex] = []; // new row in the map.
    for (var columnIndex:uint = 0; columnIndex < row.length; columnIndex++) {
      if (row[columnIndex] == WALL) {
        map[rowIndex][columnIndex] = new Wall();
      } else if (row[columnIndex] == WALL2) {
        map[rowIndex][columnIndex] = new Wall2();
      } else {
        trace("Unknown indicator.")
      }
    }
  }
  return map;
}

您基本上使用简单的占位符/指标解析通用数组,然后