Nigh7Sh4de
Nigh7Sh4de

Reputation: 455

ActionScript 3 Array Return Runtime Error

All right so I have the following code and all it does is put 3 solid-colour squares on the screen and one rainbow-coloured one in the bottom-right corner. When the user presses on any of the solid-coloured squares, that spot get filled with the rainbow-coloured one and in the original location of the rainbow goes that square that was clicked. The code works almost perfectly except for one thing. When the user tries to click on a square that is UNDER the rainbow square, it returns a run-time error.

My Code:

i

mport flash.display.DisplayObject;
import flash.ui.Mouse;

var t1:DisplayObject = new mc_1;
var t2:DisplayObject = new mc_2;
var t3:DisplayObject = new mc_3;
var t4:DisplayObject = new mc_4;

var tile:Array = [[t1,t2],[t3,t4]];

var r:int;
var c:int;
var a:int = 50;
var b:int = 50;
var aa:int = 1;
var bb:int = 1;
function reDraw() {
    a = 50;
    b = 50;
    for (r=0;r<2;r++) {
        for (c=0;c<2;c++) {
            tile[r][c].x = a;
            tile[r][c].y = b;
            trace(tile[r][c]);
            stage.addChild(tile[r][c]);
            tile[r][c].addEventListener(MouseEvent.CLICK, go);
            a += 100;
        }
        a = 50;
        b += 100;
    }
}
reDraw();

function go(e:MouseEvent):void {
    trace(e.target);
    //Right:
        if (e.target == tile[aa][bb+1]) {
            tile[aa][bb] = e.target;
            bb += 1;
            tile[aa][bb] = t4;
            reDraw();
            trace("Right");
        }
    //Left:
        else if (e.target == tile[aa][bb-1]) {
            tile[aa][bb] = e.target;
            bb -= 1;
            tile[aa][bb] = t4;
            reDraw();
            trace("Left");
        }
    //Up:
        else if (e.target == tile[aa-1][bb]) {
            tile[aa][bb] = e.target;
            aa -= 1;
            tile[aa][bb] = t4;
            reDraw();
            trace("Up");
        }
    //Down:
        else if (e.target == tile[aa+1][bb]) {
            tile[aa][bb] = e.target;
            aa += 1;
            tile[aa][bb] = t4;
            reDraw();
            trace("Down");
        }
        else trace("FAILED!");
    trace(aa +" " +  bb);
}

The error:

TypeError: Error #1010: A term is undefined and has no properties. at win_fla::MainTimeline/go()

Upvotes: 0

Views: 110

Answers (1)

Elijah Vankov
Elijah Vankov

Reputation: 72

If you take a look at your code you have this:

//Down:
    else if (e.target == tile[aa+1][bb]) {
        tile[aa][bb] = e.target;
        aa += 1;
        tile[aa][bb] = t4;
        reDraw();
        trace("Down");
    }

now you can see here that its looking for tile[aa+1] however aa = 1 in the beginning so aa+1 = 2 and tile[2] doesn't exist or is undefined. You'll need to change your logic there to something like:

var tileFound:Boolean = false;
for(var i:int = 0; i < 2; i++){
    for(var j:int = 0; j < 2; j++){
        if(tile[i][j] == e.target){
            tileFound = true;
            tile[aa][bb] = e.target;
            tile[i][j] = t4;
            if(i > aa) trace ("Right");
            else if(i < aa) trace ("Left");
            if(j > bb) trace ("Bottom");
            else if(j < bb) trace ("Top");
            aa = i;
            bb = j;
            reDraw();
            tileFound = true;
            break;
        }
    }
    if(tileFound) break;
}

Upvotes: 2

Related Questions