views:

24

answers:

2

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;
}
A: 

splice returns an array so you should try

this.addChild(colArray.splice(1, 0, playerObject)[0]);

or better

colArray.splice(1, 0, playerObject);
this.addChild(playerobject);
maxmc
That didn't work at all. Still the same error.
jtdino
A: 

Do you need to replace element #1 in the array? Or are you trying to replace element #1 so the new object appears near the top of your array? Element 0 is the actual, first element in any array....

By using the code below, you are actually just deleting the element. By using "0" for your second argument, you are telling the method to REMOVE the array element at index "1", so, I believe the additional argument (playerObject) is just getting ignored:

colArray.splice(1, 0, playerObject);

If you wanted to simply ADD the element, I would use the push() method:

colArray.push(playerObject);

If you wanted to remove the element at index 1, then use:

colArray.splice(1, 0);

Also, by using the splice method, you are actually begin passed a result. It is not an array (unless you are removing multiple elements). It is the contents of the array at that particular index.

So, if you wanted to remove the item from your array and then add it to the stage:

playerObject = colArray.splice(1, 0);
this.addChild( playerObject );

If there is a problem instantiating the object as a MovieClip you can coerce it to a specific type of object since Flash allows for multiple types of Objects: Object, MovieClip, DisplayObject...

this.addChild( MovieClip( playerObject ) );

or try.... this.addChild( playerObject as MovieClip );

or try.... this.addChild( playerObject as DisplayObject );

Hope this helps.

exoboy
Thanks for your well thought out reply. The code I pasted as an example is a test script. I want to replace an element with an playerObject in my colArray, but I don't want to just replace element #1. I tried trace(colArray[0][1]); but the output was property or method of a null object reference. Since that isn't returning anything, I can't splice into the Array. This is what I think it might.
jtdino