简体   繁体   English

as3变量名称和实例名称之间有什么区别?

[英]as3 what is the difference between the variable name and the name of an instance?

I stumbled across that while using the "this" keyword inside of my classes: 我在类中使用“ this”关键字时偶然发现了这一点:

i have one MovieClip class that is added to the stage. 我有一个添加到舞台上的MovieClip类。 from inside its instance seems to know the name i assigned to it in the properties (chapter1). 从其实例内部看来,我知道我在属性(chapter1)中为其分配的名称。

then it (chapter1) creates an instance of another class (Transition) into a variable (transition1): 然后,它(第1章)将另一个类(过渡)的实例创建为变量(过渡1):

var transition1:Transition = new Transition();

then i call an interior method of transition1 like this: 然后我这样调用transition1的内部方法:

transition1.moveFromTo(this);

which is is tracing its name like this: 正在像这样跟踪其名称:

trace(this.name);

while tracing the name of chapter1 i get "chapter1" as its name. 在跟踪第1章的名称时,我得到“ chapter1”作为其名称。 but inside of transition1 i get "instance1" through the very same way. 但是在transition1内部,我通过相同的方式得到“ instance1”。 i already figured out that i can assign the name via "transition1.name = transition1". 我已经知道我可以通过“ transition1.name = transition1”来分配名称。 That shows me that the variable name and the instance name i get through the .name property differ. 这表明我通过.name属性获得的变量名称和实例名称不同。

but I dont understand why I then have to create a variable to reference the instance. 但是我不明白为什么我随后必须创建一个变量来引用实例。

The instance name is a way of referring to the movieclip. 实例名称是引用动画片段的一种方式。
The variable is the way to access functions, variables applicable to that movieclip. 变量是访问函数的方法,这些函数适用于该动画片段。

var currentTile:MovieClip = this.getChildByName("MyClip") as MovieClip;
currentTile.someFunction();

From here you can manipulate the movieclip. 从这里您可以操纵动画片段。 If you created a variable, private to the chapter1 class, you would not have to name the movieclip as the scope of the variable would cover all the code in the class. 如果创建了一个对Chapter1类私有的变量,则不必命名movieclip,因为变量的范围将覆盖该类中的所有代码。

When you add a movieclip to the stage at design time, it seems to me flash assigns a variable called 'instancex' and names it 'instancex'. 当您在设计时将动画片段添加到舞台上时,在我看来Flash会分配一个名为“ instancex”的变量并将其命名为“ instancex”。 When you name it, it changes the names. 命名时,它会更改名称。
There is also no need to name the movieclip if the variable is within the scope of your code or if you dont have to refer to it at a later stage. 如果变量在代码范围之内,或者如果您以后不必引用它,则也无需命名movieclip。
Another way of looking at it. 另一种看待它的方式。 The 'variable' is an object containing properties, functions. “变量”是一个包含属性,函数的对象。 The 'instance name' is a property of the object. “实例名称”是对象的属性。
I hope it helps. 希望对您有所帮助。

name is a property of DisplayObject . nameDisplayObject的属性。

When you fill out the 'instance name' field in MovieClip properties via the Flash IDE, what actually happens is you give that MovieClip a value for name . 当您通过Flash IDE在MovieClip属性中填写“实例名称”字段时,实际上发生的是您为MovieClip赋予name值。 Additionally, there is a variable created for you to use with the same name. 此外,还会创建一个变量,以供您使用相同的名称。 It's essentially a shortcut so that you don't have to do something like this for all your MovieClips on the stage: 从本质上讲,这是一种快捷方式,因此您无需在舞台上的所有MovieClip上执行以下操作:

var myclip:MovieClip = getChildByName("myclip");

If you do no assign a name to a DisplayObject, it is automatically given a name, which will be something like instance3 . 如果您没有为DisplayObject分配名称,则会自动为其指定一个名称,该名称将类似于instance3 Try this code for example: 请尝试以下代码,例如:

var mc:MovieClip = new MovieClip();
trace(mc.name); // instance1

In these cases, there is no variable created for you to use, and you'll have to create a reference to it yourself. 在这些情况下,不会创建供您使用的变量,而您必须自己创建对该变量的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM