0

EDIT: I figured this problem out on my own, and have included the answer below.

I have a variable in my main timeline called characterDismissed which is a Boolean. I also have a series of nested MovieClips (MovieClips within MovieClips) which look something like: Stage > Container > List > Buttons.

In the Buttons MovieObject at the bottom of the nest I'm trying to output characterDismissed's value just to see if it can see or modify it:

trace("characterDismissed is: " + characterDismissed);

This obviously doesn't work, and I understand why it doesn't work (because characterDismissed is not a variable in the Buttons ActionScript, but rather in the main timeline's ActionScript, so it has no concept of the characterDismissed variable yet.)

How would I go about making this variable accessible to the Buttons MovieClip in AS3? I've tried root.characterDismissed, parent.characterDismissed, this.parent.characterDismissed, even parent.parent.parent.characterDismissed, etc. These always give me some flavor of this error, however:

1119: Access of possibly undefined property characterDismissed through a reference with static type flash.display:DisplayObjectContainer.

I feel like I've been reading suggestions for handling this for days, but nothing is working, and with my understanding of AS3 being limited already, I don't have a proper grasp on the vocabulary to better research it past what I've already searched, or make sense of what typically ends up being a vague response on other forums, or for similar, but not-quite-right questions/answers.

4

1 回答 1

1

我最终自己找出了答案,这就是我想出的:

我创建了一个新的 ActionScript 3.0 类文件并将其命名为 GlobalVars(不过,您可以随意命名。)并将它保存到我的项目目录中,与我的主 .FLA 文件一起。在 GlobalVars 中,我创建了一个名为 testVar 的测试变量,将其设置为 public,然后是静态的。

我对此的理解是 public 意味着任何东西都可以修改它,而 static 意味着这个变量在整个程序中都是相同的值。看起来像这样:

public static var testVar:Number = 1234;

然后在我的主项目 AS3 和嵌套对象的 AS3 中,我添加了:

import GlobalVars;

这会将我创建的类以及我在 GlobalVariables 中配置的任何函数或变量添加到时间轴上的主 AS3 脚本中。

现在,我可以通过简单地在变量前面加上类名来访问或更改这些 AS3 脚本中的变量,如下所示:

GlobalVars.testVar += 20; // Add 20 to testVar.

现在,只要我将 GlobalVars 导入到我的脚本中,我就可以从任何地方访问和修改这些变量。

希望这可以帮助那些发现自己缺乏正确表达对该主题的搜索的词汇的其他人。我试图在我的解释中包含尽可能多的关键字,以帮助具有类似搜索查询的人。

于 2013-10-24T07:03:13.393 回答