0

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()

8
  • go() is not defined... timeline script?? Commented Oct 19, 2012 at 12:37
  • all of this code is on the timeline under frame 1 Commented Oct 19, 2012 at 12:39
  • You have to provide that method - it is missing. Commented Oct 19, 2012 at 12:51
  • do you have a line fault? I bet that tile[aa][bb+1] can be problem when bb+1 is greater or equal 2. Commented Oct 19, 2012 at 12:52
  • Well as you can see i have a trace command that tells me what bb and aa are, and when the rainbow is in the top row it returns "0 0" so [aa][bb+1] is equal to [0][1] which is (at least I think it is) a valid statement? Commented Oct 19, 2012 at 13:02

1 Answer 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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.