I am trying to replace an Object inside my nested array (colArray) by using splice, the object acts as my player and will need to maneuver around the Array it's in. The Problem is splice doesn't appear to be showing anything in return, it comes up with an error saying: Cannot access a property or method of a null object reference.
What am i doing wrong ? And how would i go with moving my playerObject around the array? I have tried approaching this problem many times tonight, if some of my code is inefficient please correct me. thanks in advance.
var gridContainerMC:MovieClip = new MovieClip();
var gridSize:Array = [col,row]; //Rows, Columns
var gridArray:Array = new Array();
var col:Number = 44;
var row:Number = 33;
var mapWidth:Number = 800; // set size correct
var mapHeight:Number = 600;
var sepCols:Number;
var sepRows:Number;
var menuHeight:int = 2;
// remove rows for menu
gridSize[1] = row-menuHeight;
var colArray:Array
buildGrid();
//posPlayer();
function buildGrid() {
sepCols = mapWidth / col;
sepRows = mapHeight / row;
// declare increment varibles
var i:int;
var n:int;
// creates nested Array
for (i = 0; i < gridSize[0]; i++) {
colArray = new Array();
for (n = 0; n < gridSize[1]; n++) {
var tileObject:MovieClip = createClip();
tileObject.x = i * (tileObject.width - 1);
tileObject.y = n * (tileObject.width - 1);
gridContainerMC.addChild(tileObject);
colArray.push(tileObject);
}
gridArray.push(colArray);
}
this.addChild(gridContainerMC);
var playerObject:MovieClip = createCharacter();
this.addChild(colArray.splice(1, 0, playerObject));
trace(playerObject);
} // endOf BuildGrid
function createClip ():MovieClip {
var returnClip:MovieClip = new MovieClip();
returnClip.graphics.lineStyle(1,0x8C8C8C);
returnClip.graphics.beginFill(0xc2c2c2, 1);
returnClip.graphics.drawRect(0, 0, sepCols, sepRows);
returnClip.graphics.endFill();
return returnClip;
} // endOf createClip
function createCharacter():MovieClip {
var playerClip:MovieClip = new MovieClip();
playerClip.graphics.lineStyle(1, 0x999999);
playerClip.graphics.beginFill(0x000000); //990033
playerClip.graphics.drawRect(0, 0, sepCols, sepRows);
playerClip.graphics.endFill();
return playerClip;
}