简体   繁体   中英

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". Each time a new instance is pushed into an Array "apples". I call the function 5 times and I get 5 apples. I can manipulate them by calling them ie apples[0]. And when I trace my array I see 5 [object Apple] things. So maybe I don't really understand the structure of AS3 objects but shouldn't each object have a name?

When I set apple.name and get an array with 5 different names, I can't manipulate objects by names like 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]? And if I create a code that should be same for all apples, how should I address the function, to "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. 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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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