为什么AS3在for循环中创建新实例? [英] Why is AS3 creating new instances in for loop?

查看:118
本文介绍了为什么AS3在for循环中创建新实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经从动作脚本中看到了最奇怪的行为,并且与AS合作已有10多年了.

Strangest behavior I have seen from actionscript so far and I have been working with AS for over 10 years.

var clip1:MovieCip = new MovieClip();  
var clip2:MovieCip = new MovieClip();  
var clip3:MovieCip = new MovieClip();    
var clip;

var myarray:Array = new Array(clip1, clip2, clip3);    

for each (clip in myarray)
{ removeChild(clip);
  clip = new mc();
  trace(clip.name); }

似乎很简单,对吧?由于某种原因,flash会将实例名称更改为instanceX,其中X是一些随机分配的数字,并且我不再能够通过其分配的名称调用剪辑,例如,如果我尝试...

seems simple enough right? well for some reason flash is changing the instance names to instanceX where X is some randomly assigned number and I am no longer able to call on the clips by their assigned names, for example if I try...

 clip1.x = 300;

flash不会引发错误,但是clip1.x不会移至300.我已经处理了几个小时,看来影片剪辑实际上仍然存在,但是flash创造了新的影片剪辑!请帮助

flash will not throw an error but clip1.x does not move to 300. I have been working on this for a few hours, it seems the movieclips actually still exist but flash has created new movieclips! Please help

推荐答案

似乎您不了解成员名称和实例名称之间的区别.我假设您在时间轴上编写代码,这意味着您在该时间轴表示的 MovieClip 对象中进行操作(可能是主时间轴,在这种情况下,您将在根目录中进行操作).

It seems you don't understand the difference between member names and instance names. I assume that you write code on the timeline, that means you operate inside the MovieClip object that is represented by that timeline (might be the main timeline, in this case you are operating inside the root).

成员是一个OOP词,表示对象的字段(纯数据和对象引用)和方法(绑定函数).您可以使用点符号或方括号符号来访问成员:

Member is an OOP word that represents fields (plain data and object references) and methods (bound functions) of the object. You can use dot notation or square bracket notation to access the members:

this['a'] = 10;
trace(this.a); // output: 10

在时间轴上编写代码时,声明变量或函数时,实际上是在分别声明当前 MovieClip 的字段和方法:

When you are writing code on timeline, when you declare variables or functions, you are actually declaring fields and methods (respectively) of the current MovieClip:

var a:int = 10;

trace(a);         // output: 10
trace(this.a);    // output: 10
trace(this['a']); // output: 10

请记住,在函数体内声明的变量不是对象成员,而是局部函数变量:

Keep in mind that variables, declared inside function bodies, are not object members but the local function variables:

function A():void
{
    var a:int = 999;

    trace(a);      // output: 999
    trace(this.a); // output: undefined
}

实例名称是 DisplayObject 类的类成员,位于名称"名称下.

Instance names are class members of DisplayObject class under a "name" name.

trace(name);         // output: instance123
trace(this.name);    // output: instance123
trace(this['name']); // output: instance123

与此有关的令人困惑的部分是,Flash会自动将预先设计好的内容声明为对象成员,并且其成员名称与实例名称相同.因此,如果您将实例名称为 MC1 MovieClip 放入,则可以按以下方式进行寻址:

The confusing part about it is that Flash auto-declares pre-designed things as the object members with the member names same as instance names. So, if you put some MovieClip with the instance name MC1, you can address it as following:

trace(MC1);         // output: [object MovieCip]
trace(this.MC1);    // output: [object MovieCip]
trace(this['MC1']); // output: [object MovieCip]
trace(getChildByName("MC1"));      // output: [object MovieCip]
trace(this.getChildByName("MC1")); // output: [object MovieCip]

您可以在文件>发布设置> AS3设置>自动声明舞台实例中关闭自动声明选项.如果这样做,输出将有所不同:

You can turn the auto-declare option off at File > Publish Settings > AS3 Setings > Auto-declare Stage Instances. If you do so, the output will be different:

trace(MC1);         // output: undefined
trace(this.MC1);    // output: undefined
trace(this['MC1']); // output: undefined
trace(getChildByName("MC1"));      // output: [object MovieCip]
trace(this.getChildByName("MC1")); // output: [object MovieCip]

此外,该自动声明的东西起作用了,让我再强调一次,仅使用预先设计的内容.如果您创建任何东西的新实例并将它们 addChild(...)添加到容器中,它将不会自动将其引用添加为容器OOP成员.

Also, that auto-declare thing works, let me stress it again, only with pre-designed content. If you create new instances of anything and addChild(...) them to the container, it won't automatically add their references as the container OOP members.

然后,它如何影响您的问题.

Then, how it affects your problem.

您致电 clip1.x = 300; ,并且没有错误.当然没有. clip1 成员仍然保留对最初声明和实例化的 var clip1:MovieCip = new MovieClip(); 的引用,并且您从不会将任何内容重新分配给该成员.因此,您可以解决未附加任何内容但对AS3或Flash平台无关的有效 MovieClip :您正在对有效对象执行有效操作.

You call clip1.x = 300; and there's no error. Of course there are none. The clip1 member still holds the reference to the originally declared and instantiated var clip1:MovieCip = new MovieClip(); and you never reassigned anything to this member. So you address the valid MovieClip that is not attached to anything, but that does not matter to AS3 or Flash platform: you are performing a valid operation on valid object.

如果要处理新创建的实例,则应通过放置它们的数组或通过精心组织的实例名称或通过将其引用分配给各个成员名称的成员来实现:

If you want to address the newly created instances, you should do it via the array you put them to, or via carefully organized instance names, or by assigning their references to the members of respective member names:

var clip0:MovieCip = new MovieClip;
var clip1:MovieCip = new MovieClip;
var clip2:MovieCip = new MovieClip;
var clip3:MovieCip = new MovieClip;

var myarray:Array = new Array(clip0, clip1, clip2, clip3);

for (var i:int = 0; i < myarray.length; i++)
{
    // You better call the class MC, because lowercase
    // class name looks like a variable name.
    var aClip:MC = new MC;

    // Give it a proper instance name.
    aClip.name = "clip" + i;

    // Assign it as a member of current object.
    this["clip" + i] = aClip;

    // Put it to designated place into the Array.
    myarray[i] = aClip;
}

现在,您可以按任何方式解决它:

Now you can address it any way you want:

trace(clip1);         // output: [object MC]
trace(this.clip1);    // output: [object MC]
trace(this['clip1']); // output: [object MC]
trace(myarray[1]);    // output: [object MC]
trace(getChildByName("clip1"));      // output: [object MC]
trace(this.getChildByName("clip1")); // output: [object MC]

这篇关于为什么AS3在for循环中创建新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆