1
votes
package 
{

import flash.events.Event;
import flash.display.MovieClip;

// class 
public class GameGrid extends MovieClip
{
    private var gameHeight:Number = 600;
    private var gameWeight:Number = 800;
    private var gridHeight:Number = 50;
    private var gridWeight:Number = 50;
    private var rowNumber:int = 12;
    private var columnNumber:int = 16;
    private var backgroundGrid:Array = new Array(12,16);
    private var foregroundGrid:Array = new Array(12,16);

    function GameGrid(){

    }

    function addBackGrid(rowN:int,colN:int,mcObject:MovieClip)
    {
        backgroundGrid[rowN,colN].push(mcObject);
    }

    function addForeGrid(rowN:int,colN:int,mcObject:MovieClip)
    {
        foregroundGrid[rowN,colN].push(mcObject);
    }

    function calculateRowDiff(rowA:int,rowB:int):Number
    {
        return Math.abs(rowA-rowB);
    }
    function calculateColDiff(colA:int,colB:int):Number
    {
        return Math.abs(colA-colB);
    }
    function calculateCorDiff(colA:int,colB:int,rowA:int,rowB:int):Number
    {
        return Math.sqrt((calculateRowDiff(rowA,rowB) * calculateRowDiff(rowA,rowB)) + (calculateColDiff(colA,colB) * calculateColDiff(colA,colB)));
    }
    // add to stage
    function paintbackgroundGrid()
    {
        for (var i:int=0; i<16; i++)
        {
            for (var j:int=0; j<12; j++)
            {
                MovieClip(backgroundGrid[i,j]).x = i * 50;
                MovieClip(backgroundGrid[i,j]).y = j * 50;
                stage.addChild(MovieClip(backgroundGrid[i,j]));
            }
        }
    }
}


}

So what this GameGrid class do is to hold an Array of grids(or tiles which extends MovieCLip) that will be added to the main stage and will call the initializeItem function.

function InitializeItem(e:Event)
    {
        var gamemap = new GameGrid();
        var mc:MovieClip = new MainCharacter();
        gamemap.addBackGrid(1,1,mc);
        gamemap.paintbackgroundGrid();
        //trace("Year: "+gameTime.gameYear+" Month: "+gameTime.gameMonth+" Day: "+gameTime.gameDay+"  "+gameTime.gameHour+":"+gameTime.gameMinute+":"+gameTime.gameSecond);

    }

The initializeItem should create an instance of gamegrid, and add movieclips to their respective locations(stored using array) and display them. and this is the error stacktrace:

ReferenceError: Error #1069: Property 1 not found on Number and there is no default value. at GameGrid/addBackGrid()

The debugger suggest that the error came from the line backgroundGrid[rowN,colN].push(mcObject); Is there a way I can hold a 2d array movieclips? I'm new to AS3 and it looks very similar to JAVA, what am I missing?

1

1 Answers

2
votes

Try this

private var backgroundGrid = [];

function addBackGrid(rowN:int,colN:int,mcObject:MovieClip) {

    if (backgroundGrid[rowN] == null) {

         backgroundGrid[rowN] = [];

    }

    backgroundGrid[rowN][colN] = mcObject;
}

In as3, following code means create a array and the array contains two elements, one is 12, and the other is 16.

 private var backgroundGrid:Array = new Array(12,16);