Search code examples
actionscript-3flash-cs4

as3 Access to undefined property?


Can someone help me to find out why I'm getting the error message "Access to undefined property: removeChild(goBack)" on the following snipped?

BTW, this is for flash CS4

function nameOfFunction() {

var goBack:backButton_mc = new backButton_mc();

goBack.x = 10;

goBack.y = 700;

goBack.back_text.text = myXML.*[buildingName].NAME;

goBack.name = "backBtn";

goBack.buttonMode = true;


addChild(goBack);

goBack.addEventListener(MouseEvent.CLICK, anotherFunction);

}


function anotherFunction(e:MouseEvent):void {

 removeChild(goBack);

}

Solution

  • You are wrong with the scope. (surprise :-D)

    The variable goBack is just defined inside of "nameOfFunction", when you try to access this from a another function like "anotherFunction" it will not exists anymore (even if it is on the display list)

    There are different possibilities to solve this problem:

    function anotherFunction(e:MouseEvent):void {
       removeChild(e.currentTarget);
    }
    

    Or the best way would be: promote goBack as a class member of the class holding both functions. (Or if you don't use classes make goBack "global".)