0

好的,所以我有以下代码,它所做的只是在屏幕上放置 3 个纯色方块,在右下角放置一个彩虹色方块。当用户按下任何纯色方块时,该点会被彩虹色的方块填充,并且在彩虹的原始位置上会出现被点击的方块。除了一件事之外,该代码几乎可以完美运行。当用户试图点击彩虹方块下的方块时,它会返回一个运行时错误。

我的代码:

一世

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);
}

错误:

TypeError:错误#1010:术语未定义且没有属性。在 win_fla::MainTimeline/go()

4

1 回答 1

2

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;
}
于 2012-10-19T13:08:47.197 回答