0

看看这段代码:

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

好吧,我刚刚创建了一个名为mc的影片剪辑并将其添加到舞台上。既然每个影片剪辑都是用相同的名称“ mc ”创建的,为什么我可以通过将单个影片剪辑推入一个数组来处理它?就像,balls[0] 将是我的第一个电影剪辑。为什么我不能创建:

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

然后把每一个球都当作ball[0], ball[1], 等等...?为什么我必须将它添加到数组或为每个数组分配不同的名称,使其独一无二?

另外,如何从一个自动创建球的类中创建一个对象,然后当我删除这个对象时,我也删除了这个球?

我想了解电影剪辑在 AS3 中的确切含义。

谢谢

4

2 回答 2

0

我已尽力解释和回答您的问题。如果您觉得我的回答的第一部分过于评论,请坚持阅读并阅读,它将帮助您理解其余部分。所有这些都在代码中 - 所以请仔细阅读评论。

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.

下面是球类:

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();
    }
}
}

要使用它,我们只需像创建任何其他对象一样创建它,还要将参数传递给它的构造函数。我们不必传递参数,但如果我们不希望将这些默认尺寸和颜色应用于我们的球,那么我们最好用不同的替换它们。

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.

请,如果您的问题已得到解答,请接受此答案并可能投票,但如果没有,请回答更详细的问题,我可以更好地理解并更好地解释答案。谢谢

于 2013-10-22T00:58:57.970 回答
0

是这样

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

或者

balls.push(new ball());

很简单,不是吗?

于 2013-10-22T01:39:24.133 回答