[英]AS3 class instances names
I really wonder. 我真的很好奇 I made a MovieClip class Apple, I wrote a function that creates a new instance with a name "apple". 我制作了一个MovieClip类Apple,并编写了一个函数来创建一个名为“ apple”的新实例。 Each time a new instance is pushed into an Array "apples". 每次将新实例推入“苹果”数组中。 I call the function 5 times and I get 5 apples. 我调用该函数5次,然后得到5个苹果。 I can manipulate them by calling them ie apples[0]. 我可以通过调用它们来操纵它们,例如apples [0]。 And when I trace my array I see 5 [object Apple] things. 当我跟踪数组时,我看到了5个[Apple Apple]东西。 So maybe I don't really understand the structure of AS3 objects but shouldn't each object have a name? 因此,也许我不是很了解AS3对象的结构,但是每个对象都不应该有名称吗?
When I set apple.name and get an array with 5 different names, I can't manipulate objects by names like apple1.x = 10 . 当我设置apple.name并获得具有5个不同名称的数组时,我无法通过apple1.x = 10之类的名称来操作对象。 How does computer know which apple is where if each has own coordinates? 如果每个人都有自己的坐标,计算机如何知道哪个苹果在哪里? Is the only way to call them: apples[0]-apples[4]? 是调用它们的唯一方法:apples [0] -apples [4]? And if I create a code that should be same for all apples, how should I address the function, to "this"? 如果我创建的代码对于所有苹果都应该相同,那么该如何将该函数寻址为“ this”? Cause when I write class code I don't have any names yet... 原因是当我编写类代码时,我还没有任何名字...
For example, if I want to make Apple class a picture(MovieClip) that can be dragged, create any number of apples, up to a million, I can't possibly add apples[0].addEventListener, apples[1].addEventListener ... apples[1000000].addEventListener to the code. 例如,如果我想让Apple类成为可拖动的图片(MovieClip),创建任意数量的苹果(最多一百万个),则可能无法添加apples [0] .addEventListener,apples [1] .addEventListener ... apples [1000000] .addEventListener到代码。 How do I make it global? 我如何使其全球化?
I'm asking cause when I'm directly coding for a specific instance, it has a name and I know exactly what am I addressing. 我问原因,当我直接为特定实例编码时,它有一个名称,我确切地知道我要解决的是什么。 And working with a class and making many objects I kinda don't... Sorry, I'm green 而且与一个班级一起工作并且制作很多我不太喜欢的对象...对不起,我很绿色
You don't need names to be able to access and manipulate your instances. 您不需要名称即可访问和操作您的实例。
You can do something like this: 您可以执行以下操作:
var apples:Array = new Array();
for (var i:int = 0; i < 100; i++)
{
var apple:Apple = new Apple();
apples.push(apple);
apple.x = Math.random() * 500;
apple.y = Math.random() * 500;
addChild(apple);
apple.addEventListener(MouseEvent.CLICK, onAppleClick);
}
function onAppleClick(e:MouseEvent):void
{
var ind:int = apples.indexOf(e.currentTarget);
var apple:Apple = apples[ind];
trace(ind);
apples[ind].visible = false;
//or
//apple.visible = false;
}
The same problem, huh? 同样的问题,对吧? :) :)
For example Apple extends Sprite
and inherits property name
. 例如, Apple extends Sprite
并继承了属性name
。
It's public, this means that you can change its name. 它是公开的,这意味着您可以更改其名称。
var apple:Apple = new Apple() ;
apple.name = "MegaApple" ;
Also, if you add the apple to stage, you can get him via name. 另外,如果您将苹果添加到舞台上,则可以通过名称获取他。
stage.addChild(apple) ;
get apple back: 找回苹果:
var oldApple:Apple = stage.getChildByName("MegaApple") ;
But that never means, that you can use apple like that: MegaApple.move()
- because name of apple is supposed to be a property of apple, not a name of a variable. 但这绝不意味着您可以这样使用apple: MegaApple.move()
-因为apple的名称应该是apple的属性,而不是变量的名称。
Of course, you can create a lot of apples manually: 当然,您可以手动创建很多苹果:
var apple1 = new Apple() ;
var apple2 = new Apple() ;
var apple3 = new Apple() ;
But if they all behave the same way, there is no point in doing. 但是,如果它们的行为方式都相同,那就没有意义了。 That's why you store them into array. 这就是为什么将它们存储到数组中的原因。
Hope you understand what I mean. 希望你明白我的意思。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.