AS3 - 关于电影剪辑

时间:2013-10-21 23:08:19

标签: actionscript-3 flash movieclip

看看这段代码:

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

好吧,我刚创建了一个名为 mc 的影片剪辑并将其添加到舞台上。由于每个动画片段都使用相同名称“ mc ”创建,为什么我只需将其推入数组即可处理各个影片剪辑?就像,球[0]将成为我的第一部电影剪辑。为什么我不能创建:

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

然后对待每一个球,如ball[0]ball[1]等......? 为什么我必须将它添加到数组中或为每个数组指定一个不同的名称,使其唯一?

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

我想了解一下动画片段在AS3中的含义。

感谢

2 个答案:

答案 0 :(得分: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.

如果您的问题已得到解答,请接受,然后接受这个答案,也许可以投票,但如果没有,那么请回答更详细的问题,我可以更好地理解,然后更好地解释答案。感谢

答案 1 :(得分:0)

就是这样

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

balls.push(new ball());

简单,不是吗?