Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Dec 08, 2010 Dec 08, 2010

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

}

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

TOPICS
ActionScript
536
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Dec 09, 2010 Dec 09, 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.

Translate
LEGEND ,
Dec 09, 2010 Dec 09, 2010
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines