Sauce Alfredo Sauce Alfredo - 9 months ago 84
ActionScript Question

Why when I Update a 2 dimensional Array it give an "Error #1010"

In my programming class,

I need to create a Tiles game with AS3 (like zelda).

On a map, the tiles are initialised black in an array and, after that, they are changing randomly each time the leftpick of a song reach a certain value.




When the tiles change, I need to update the Array with the new value. Actually, I always get an Error #1010: A term is undefined and has no properties.






This is how the tiles are initialised:


variable


private var grid: MovieClip;
private var nbRow: int = 6;
private var nbCol: int = 12;
private var oneTiles: Tiles;
private var t: Tiles;

public var tMap: Array = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];



Creation of the map


private function createGrid(): void {
grid = new MovieClip();
addChild(grid);

for (var r: int = 0; r < nbRow; r++) {
for (var c: int = 0; c < nbCol; c++) {
t = new Tiles();
t.x = t.width * c;
t.y = t.height * r;
grid.addChild(t);
}
}
}



Set Tiles


private function displayTiles(): void {
var i: int = 0;
for (var r: int = 0; r < nbRow; r++) {
for (var c: int = 0; c < nbCol; c++) {
var t: Tiles;
t = Tiles(grid.getChildAt(i));
t.gotoAndStop(tMap[r][c]);
i++;
}
}
}



This is the function called everytime the leftpeak reach the value


private function resetTiles(): void {
for (var i: int = 0; i < grid.numChildren; i++) {
oneTiles = grid.getChildAt(i) as Tiles;

oneTiles.getTiles();
//30% chance gotoAndStop(1) black tiles
//70% chance gotoAndStop(2) white tiles
}
}



This is the source of the problem : to update the array, I added this in the for loop of resetTiles(). Whit it, I always get an Error #1010: A term is undefined and has no properties. :


private var posX: uint = 0; //global
private var posY: uint = 0; //global

tMap[posX][posY] = oneTiles.getFrame();

if(posX == 11 && posY != 5){
posX = 0;
posY++;
}else if(posX == 11 && posY == 5){
posX = 0;
posY = 0;
}else{
posX++;
}
trace(posX);
trace(posY);

}




  • So, where's the problem ? Normaly, with this code, each time a tile is changed, the good tile in tMap shall be updated.

  • I did some test, and what seems to be the source of the problem is the line tMap[posX][posY] = oneTiles.getFrame(); Still, I can't figure out why




public function getFrame():void{
this.currentFrame;
}

Answer Source

Let me explain how to deal with this kind of problem. As soon as you learn that a specific line gives you trouble, you need to understand the whole picture. You should learn the current state and value of each object involved, something like that:

import flash.utils.getQualifiedClassName;

function hyperTrace(prefix:String, target:*):void
{
    trace("");
    trace("Name:", prefix);
    trace("Value:", target);
    trace("Type:", typeof(target));
    trace("Class:", getQualifiedClassName(target));
}

And then you start learning:

// Expected result: instance of Tiles class
hyperTrace("oneTiles", oneTiles);

// Expected result: Function
hyperTrace("oneTiles.getFrame", oneTiles.getFrame);

// Expected result: Array
hyperTrace("tMap", tMap);

// Expected result: Array
hyperTrace("tMap[" + posX + "]", tMap[posX]);

// Expected result: int
hyperTrace("tMap[" + posX + "][" + posY + "]", tMap[posX][posY]);

Then, you search for the one that does not match the expected result. Some object is not what you expected there (including values of posX and posY indices) and that is the source of the error. Now that you learned where the problem is, you should figure what exactly went wrong with your previous code or development logic so that it produced the unexpected results.

Without digging deep I'd guess that either oneTiles turned out to be undefined, or posX and posY has wrong values so tMap[posX] is undefined.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download