# Variables of a child not visible to a function in the child called from its parent?

**URL:** <https://forum.kirupa.com/t/variables-of-a-child-not-visible-to-a-function-in-the-child-called-from-its-parent/261838>\
**Category:** Uncategorized\
**Created:** [May 31, 2008, 2:53pm UTC](https://forum.kirupa.com/t/variables-of-a-child-not-visible-to-a-function-in-the-child-called-from-its-parent/261838 "2008-05-31T14:53:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbnzdave](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@rbnzdave](https://forum.kirupa.com/u/rbnzdave)\
**Post date:** [May 31, 2008, 2:53pm UTC](https://forum.kirupa.com/t/variables-of-a-child-not-visible-to-a-function-in-the-child-called-from-its-parent/261838/1 "2008-05-31T14:53:31Z")

</div>

sorry, this is a repeat thread, but couldn’t change the title on the old one, someone must know the solution to this problem, I just dont think i worded it correctly enough for someone who knew what i was talking about to notice last time.

Ive got a movieclip that creates and adds a child mc then calls a function in the new mc. the function runs, but the local vars for the new mc aren’t visible to the function. Is there a special way to call a function so that its scope is in the child, not in the parent ? ( i assume thats where it is, even though a trace of this returns the correct object type )

ie:

[Parent Clip]  
–[child clip: movGraph]  
–variable ( var thegraph:MovieClip = this; )  
–[function loadMe]  
–(trace (theGraph); )

calling movGraph.loadMe spits out undefined or null object error, as opposed to  
[object movGraph]

obviously i want to do a whole lot more than trace out the object type, and trace(this) in the function returns [object movGraph]. the variable “theGraph” declared in the child isn’t available or its null somehow.

Can anyone tell me where I’m going wrong ? - any help would be appreciated. at the moment I’m having to add all the code in the function instead of the “root” of the child. which really defeats the point.

---

<div class="post-metadata">

**Author:** ![rbnzdave](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@rbnzdave](https://forum.kirupa.com/u/rbnzdave)\
**Post date:** [June 1, 2008, 2:46am UTC](https://forum.kirupa.com/t/variables-of-a-child-not-visible-to-a-function-in-the-child-called-from-its-parent/261838/2 "2008-06-01T02:46:17Z")

</div>

after more testing, I’ve found that while I can call other functions and those functions run stuff in them, nothing seems to have access to the variable data/values. if I can a variable that isn’t declared at all, it’ll spit the dummy on tracing it. it seems to recognise the type of variable, and a number set to a value when traced gives me NaN, a variable declared as a movieclip traces out as null.

Anyone able to help ?

To replicate :

create a new as3 flash file.  
create a shape and convert it to a symbol(mc) ( i call the object movGraph for this example ) - include linkage  
remove the mc from the stage  
\<AS\> ( in main stage )  
var temp:movGraph = new movGraph();  
temp.loadme();  
\</AS\>  
\<AS\> (in an actions layer in movGraph)  
var theGraph:MovieClip = this;  
var xVal:Number = 3;

function hello() {  
trace(“hello”);  
trace(xVal);  
}

function loadme():void {  
hello();

}  
\</AS\>

This, when run, will output :

hello  
NaN

---- Why ?

---

<div class="post-metadata">

**Author:** ![rbnzdave](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@rbnzdave](https://forum.kirupa.com/u/rbnzdave)\
**Post date:** [June 1, 2008, 3:00am UTC](https://forum.kirupa.com/t/variables-of-a-child-not-visible-to-a-function-in-the-child-called-from-its-parent/261838/3 "2008-06-01T03:00:12Z")

</div>

okay, dont know if this is a workaround or if this is the way its supposed to be, but if i add another function in the child clip called init(), and set the value of xVal there, then the original loadme call outputs a number…

Are variables not initialised with default values when declared in a mc that isn’t on the stage ? thats retarded…
