Skip to main content
Participant
December 9, 2010
Answered

MovieClip accessing a variable that is in a function outside of the movieclip

  • December 9, 2010
  • 1 reply
  • 533 views

I am trying to access the variable myParentVariable within the function myParentFunction() from the MovieClip's myMovieClip_mc function myMovieClipFunction().

I know that It works if I declare the variable myParentVariable outside of the myParentFunction() but I don't really want to do that, Any help would be great.

main timeline

//-----------------------------------------------------------------------

myParentFunction();

function myParentFunction():void

{

var myParentVariable:int = 50;

myMovieClip_mc.myMovieClipFunction();

}

//-----------------------------------------------------------------------

Inside MovieClip (movie clip name is myMovieClip_mc)

//-----------------------------------------------------------------------

function myMovieClipFunction():void

{

//Trying to access the variable myParentVariable

trace(MovieClip(root).myParentVariable);

trace(MovieClip(this.root).myParentVariable);

trace(MovieClip(parent).myParentVariable);

trace(MovieClip(this.parent).myParentVariable);

//None of these work!

}

//-----------------------------------------------------------------------

This work but I don't want the variable outside of the function

main timeline

//-----------------------------------------------------------------------

var myParentVariable:int = 50;

myParentFunction();

function myParentFunction():void

{

myMovieClip_mc.myMovieClipFunction();

}

//-----------------------------------------------------------------------

Inside MovieClip (movie clip name is myMovieClip_mc)

//-----------------------------------------------------------------------

function myMovieClipFunction():void

{

trace(MovieClip(root).myParentVariable);

}

//-----------------------------------------------------------------------

This topic has been closed for replies.
Correct answer Ned Murphy

If you declare the variable within the function you have limited its scope to be within the function, and nothing outside of the function will be able to determine its value.  Being inside the function, it is not a stored value either.  It is assigned in the function so it is not something you can reach in and grab the current value of.  The only way to get the value out of the function, other than declaring it outside of the function, is to have it passed as a return value from the function.

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
December 9, 2010

If you declare the variable within the function you have limited its scope to be within the function, and nothing outside of the function will be able to determine its value.  Being inside the function, it is not a stored value either.  It is assigned in the function so it is not something you can reach in and grab the current value of.  The only way to get the value out of the function, other than declaring it outside of the function, is to have it passed as a return value from the function.