从较低级别检索变量值

发布于 2024-11-30 22:02:02 字数 702 浏览 3 评论 0原文

好吧,我在主阶段级别创建了一些变量,如下所示:

for(i=0,i<10,i++){
    var var_name="var_num_"+i;
    this[var_name]="some value";
}//<-----------------------------------------------------works

所以我得到了 10 个名为“var_num0”、“var_num1”、“var_num2”的变量,每个变量都有一些值。 我可以在任何地方调用它们,

var second_var=MovieClip(root).var_num0;//<--------------works

当我想从较低级别或在另一个框架或其他地方使用另一个循环调用所有变量时,

var third_var;
for(j=0,j<3,j++){
third_var=this["MovieClip(root).var_num_"+j];//<---------DOSNT WORK
trace(this["MovieClip(root).var_num_"+j]);//<------------returns "undefined"
}

我的问题就出现了:我怎样才能完成这项工作?我尝试了很多东西,但一无所获...

谢谢大家

well i created some variables in the main stage level, with something like this:

for(i=0,i<10,i++){
    var var_name="var_num_"+i;
    this[var_name]="some value";
}//<-----------------------------------------------------works

so i get 10 variables named "var_num0", "var_num1", "var_num2" each one with some value.
and i can acces them any where calling this

var second_var=MovieClip(root).var_num0;//<--------------works

my problem comes when i want to call all the variables from a lower level or in another frame or somewhere else using another loop:

var third_var;
for(j=0,j<3,j++){
third_var=this["MovieClip(root).var_num_"+j];//<---------DOSNT WORK
trace(this["MovieClip(root).var_num_"+j]);//<------------returns "undefined"
}

how can i make this work? i tried a lot of things and nothing...

thanks you all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舟遥客 2024-12-07 22:02:02

在您的情况下,“root”和“this”都是您想要访问变量的范围。所以试试这个:

var third_var:MovieClip;
for(j = 0; j < 3; j++)
{
    third_var = MovieClip(root)[var_num_ + j];
    trace(third_var);
}

另外,你的 for 循环中应该有分号而不是角点。

In your case both "root" and "this" are the scope you want to access the vars from. so try this:

var third_var:MovieClip;
for(j = 0; j < 3; j++)
{
    third_var = MovieClip(root)[var_num_ + j];
    trace(third_var);
}

Also you should have semi-colons in your for loop rather than comers.

Saygoodbye 2024-12-07 22:02:02

我想在我的回答前建议您使用 AS3 的“文档类”,以使名称空间和继承等内容更加清晰。使用基于文档、面向对象的编程与通过 Flash IDE 进行的时间线编程(仅在 AS1/2 中才存在)相比,您可以准确地知道在哪里可以访问事物。嗯: http://www .kirupa.com/forum/showthread.php?223798-ActionScript-3-Tip-of-the-Day/page14

答案:您正在尝试移动两个级别一组 [] 中的继承 编写第一行“不起作用”的另一种方法是:

this.myMovieClip["var_num"+j"];

也可以使用: this["MovieClip"]["var_num"+j];

基本上,您需要从用于调用变量的字符串中取出“MovieClip(root)”,因为你正经过两个继承级别: this->MovieClip->targetVar

您需要使用两个句点、一个句点和一个方括号或两个方括号来移动两个继承级别 A 句点 . 和。一组方括号 [] 都完成了更深一层的任务,因此将 . 放在用于调用变量的字符串中将不起作用。

说明:

以下三个示例都返回相同的变量:

myMovieClip.my_variable

myMovieClip["my_variable"]

var str:String = "my_variable";
myMovieClip[str];

I'd like to preface my answer with a suggestion you use a 'Document Class' with AS3 to make things like namespaces and inheritance much clearer. You know exactly where things are accessible when using document based, object oriented programming versus the timeline programming available through the Flash IDE (its only there because of AS1/2). Tut: http://www.kirupa.com/forum/showthread.php?223798-ActionScript-3-Tip-of-the-Day/page14

On to the answer: You are trying to move two levels of inheritance in one set of [] Another way of writing your first "Doesn't work" line is:

this.myMovieClip["var_num"+j"];

You could also use: this["MovieClip"]["var_num"+j];

Basically, you need to take the "MovieClip(root)" out of the string you are using to call your variable because you are passing through two levels of inheritance: this->MovieClip->targetVar

You need to use two periods, a period and a set square bracket or two sets square brackets to move two levels of inheritance. A period . and a set of square brackets [] both accomplish the task of moving one level deeper, so putting the . inside the string used to call up your variable won't work.

Explanation:

The following three examples all return the same variable:

myMovieClip.my_variable

myMovieClip["my_variable"]

var str:String = "my_variable";
myMovieClip[str];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文