Question

I'm designing a chess game in ActionScript 3 using Flash Professional CC. I've created a chessboard using the IDE and placed pieces in their initial positions. Each tile has its own instance and is named its respective coordinate e.g. the top left tile is called A8.

For calculating moves that are valid and such, I planned to use two 2D arrays of objects. One array should contain the tile instances e.g. A8, B8, C8, D8 etc. and the other should contain the pieces of the board e.g. BR1, BB1.

I've noticed that ActionScript does not allow one to implement 2D arrays like C++ (a language I'm familiar with); instead, nested arrays are used. I'm slightly confused about how to set up these arrays. What is the most efficient way to declare and initialise these arrays (hopefully not involving repetitive code)?

Was it helpful?

Solution

welcome to the army of AS3 developers.

Here's few tips for you:

  • Arrays can be defined as var array:Array = []; and here's a 2d Array - var a:Array = [[]];. Arrays are dynamic object, you don't need to specify the depth of the array. So when adding tile to the array just add them via array[x][y] = tile
  • Having said that, don't use arrays :) You strictly typed collectors called Vectors. You need to define Vectors: 1d var myVector:Vector.< Tile > = new Vector.< Tile >(); and 2d version var myVector:Vector< Vector.< Tile > >; and so on.

Vectors are faster than Arrays.

More tips:

  • Saving black and write tiles in Library and constructing the grid on run-time might be better for you.
  • Accessing object on screen by their instance names is a bad idea - a lot of work and poor maintainability. Plus you won't be able to access them before the scene is finished building and you'll access them as Dynamic objects, you'll need to cast them to your needed class to work with them normally. That also leads to silent bugs since you won't get warning on compile time if you try to access something illegal.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top