简体   繁体   中英

AS3 - about movie clips

Take a look at this code:

var balls:Array = new Array();
var mc:ball = new ball();
addChild(mc);
balls.push(mc);

Well, I've just created a movie clip called mc and added it to the stage. Since every movieclip is created with the same name ' mc ', why can I work on individual movie clips just by pushing it into an array? Like, balls[0] is gonna be my first movieclip. Why couldn't I create:

for...
var balls[n]:ball = new ball();

And then treat every single ball like ball[0] , ball[1] , etc...? Why do I have to add it to an array or assign a different name for each one, making it unique?

Also, how can I create an object from a class that automatically creates a ball, and then when i delete this object, I delete the ball too?

I want to understand what exactly a movieclip means in AS3.

Thanks

I have tried as best I can to interpret and answer yours question. If you feel that the first part of my answer is too much of a review, please stick to it and read it all, it will help you understand the rest of it. All of it in inside the code - so read through the comments.

new Object();  //Creates a new object  that is now in the memory of the computer
var object:Object = new Object(); // Create a new object, and at the same time 
// assigns a reference, which is to it (object).
// This reference will allow you to refer to the created object, later in the code.
var mc:MovieClip = new MovieClip(); // Creates a advanced object called MovieClip
// MovieClips are just objects that can do more, like for instance they have the
//ability to be displayed on screen, so that the user can see them
addChild(mc) // Adds the movieclip created above to the display list, so that it is not 
//only in the computer's memory, but visible to the user
var array:Array = new Array() // Creates an Array object. This object is just like the
//MovieClip except it just can't be displayed. But unlike the MovieClip, it is a
//reference list to other objects, that you push into it.
array.push(mc);
// This command makes the first slot in the array have a reference to our mc that we
//created earliet.
array.push(new MovieClip());
// THIS is very important, this line first creates a movieclip object, but then instead
//of letting it get lost in the computer's memory, it passes it into the array.push
//function, so that its added on to the array.
// BOTH mc and array[0] now refer to the same object, changing mc.x property to 0,
//changes the array[0].x property to 0.
// To get to your question, you can bypass the variable mc completely by doing this:
array = new Array();
for(var i:int = 0; i < 10; i++) {
    array.push(new MovieClip());
    addChild(array[array.length - 1]);
}
// THIS code creates an array object, sets up a loop that will loop through 10 times;
//and on every iteration of the loop, the array adds another new MovieClip to its list,
//and then right away adds it to the display list to be viewed by the user.

// NOW to have a class automatically create a ball is a weird thing because this can
//mean so many diferent things.
// This can mean that new objects from this class, create balls that are its display
//children, or it can mean that this class is a ball itself, or that it has only one
//ball as a display child... etc.
// What I think what you mean is: how have a class automatically look like a ball,
//whenever i make an instance of it.
// This can be acheive by setting up the class the accept parameters in its
//constructor, that define how the ball will look. And by using the graphic property of
// the display object, It will set it self up to look like a ball, when it is later
// added to the display list.

Below is the ball class:

package {
import flash.display.MovieClip;

public class ball extends MovieClip { // EXTENDS MovieClip just means that it takes
//all the powers that the MovieClip class has and applies it to its self, so now this
//class is not only JUST like a MovieClip, but also has the stuff that we put into it
//in this class definition.

    private const defaultRadius:Number = 10;
    private const defaultColor:Number = 0xFF0000;

    public function ball(radius:Number=defaultRadius,color:Number=defaultColor):void {
//ALL display objects have a graphic property, this just lets you "paint" the object so
//that it has some stuff to show when it is displayed.
        this.graphics.beginFill(color, 1);
        this.graphics.drawCircle(0, 0, radius);
        this.graphics.endFill();
    }
}
}

To use it we would just create it like any other object, but also pass parameters to its constructor. We don't have to pass parameters, but if we don't want those default sizes and colors to be applied to our ball, then we better replace them with different ones.

array = new Array();
for(var i:int = 0; i < 10; i++) {
    array.push(new ball(15,0x0000FF));
    addChild(array[array.length - 1]);
}
// Creates 10 balls that have a 15 radius and are blue. We also then make them
//displayable.

Please, if you're question has been answered, then accept this answer and maybe upvote it, but if not, then please respond with a more detailed question that I can better understand and then better explain an answer. Thanks

It's this way

for...
balls[n] = new ball();

or

balls.push(new ball());

Simple, isn't it?

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