当前位置: 动力学知识库 > 问答 > 编程问答 >

actionscript 3 - as3 access VARIABLE in parent MovieClip or Loader?

问题描述:

Here is my code below. Its simple. I just need to get the var defined on the Main Stage and have every external swf get access to that var.

Here is the code that doesn't work:

IN MAIN SWF:

var ThePath:String;

ThePath = "happy/go/lucky/";

/// Below the code is in A LOADED SWF from the Main previous timeline above. (now showing full code to do this here)

var myMC:MovieClip;

myMC= parent as MovieClip;

trace("Your VAR PATH IS " + myMC.ThePath); /// DOES NOT WORK.

Thanks happy stackers!

UPDATE 4-31-12

IN SHORT: I need to make a global var in the main stage and allow all others externally loaded swf's access to that var.

网友答案:
var myMC:MovieClip = MovieClip(this.parent.parent);
myMC.ThePath

You should use camelCase as variable definitions.

EDIT:

Another solution is to pass a reference to the parent in the child swf when you load it in the loader (by setting a value, setting the reference) and then the child can reference the parent.

i.e.:

Parent:

function startLoad() { var mLoader:Loader = new Loader(); var mRequest:URLRequest = new URLRequest(Child.swf); mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler); mLoader.load(mRequest); }

function onCompleteHandler(loadEvent:Event) { var childMC:MovieClip = loadEvent.currentTarget.content; addChild(childMC:MovieClip); childMC.refToMainSWF = this; }

For reference: http://genaboo.wordpress.com/2008/03/27/as2_vs_as3_root_parent_communicating_between_swfs/

网友答案:

try:

trace("Your VAR PATH IS " + root.ThePath);
网友答案:

Create a class with static properties (variables), importing that as needed for global access. See this relevant question.

分享给朋友:
您可能感兴趣的文章:
随机阅读: