Copy link to clipboard
Copied
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);
}
//-----------------------------------------------------------------------
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.
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now