views:

3601

answers:

2

Hello,

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

}
A: 

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".)

Hippo
A: 

Hippo is correct, but I feel it is important to explain a little more.

You created a local variable, i.e. var someVariable:DataType; within a function. This means that that variable will only be available to objects in the scope (inside) of the function (local to), and it will only last for the lifetime of the function. Soon as that function has ran the code is gone until ran again. It looks like you are probable programming directly inside the flash IDE on the time-line, which is fine, but, if you were using a document class, you could merely declare you variable in the Class scope just above the constructor function, and then set the value in the same function that your using now. This way, the reference to the variable doesn't exist within the function, it is merely set from within. This will allow that variable to be accessed from anywhere in the same class even if set to private.

This may help:

//Frame 1, Actions layer
//Slap goBack right onto the root / stage
var goBack:MovieClip;
/*
I noticed you had this data-typed differently,
i prefer to type to an interface, not an implementation.
Since your class is a movieclip in the library it extends
MovieClip and therefor IS A MovieClip, but ok either way.
*/

function nameOfFunction():void
{
    goBack = 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);
}

Scope is very important and after a while very easy to tackle. Stick with it, experiment, read up on conventions and standards that can help your development and get to loving the DocumentClass becuase even though it may be daunting to some at first, once you learn it and get used to it, it so hard to go back to programming in the flash IDE on the timeline, where I believe only display objects and audio have any place being.

Brian Hodge
hodgedev.com blog.hodgedev.com

Brian Hodge