Actionscript 3 AS3-关于电影剪辑

Actionscript 3 AS3-关于电影剪辑,actionscript-3,flash,movieclip,Actionscript 3,Flash,Movieclip,请看下面的代码: var balls:Array = new Array(); var mc:ball = new ball(); addChild(mc); balls.push(mc); 嗯,我刚刚制作了一个名为mc的电影剪辑,并将其添加到舞台上。既然每个movieclip都是用相同的名称“mc”创建的,为什么我只需将其放入一个数组中就可以处理单个电影剪辑?就像,鲍尔斯[0]将是我的第一部电影。为什么我不能创建: for... var balls[n]:ball = new ball();

请看下面的代码:

var balls:Array = new Array();
var mc:ball = new ball();
addChild(mc);
balls.push(mc);
嗯,我刚刚制作了一个名为mc的电影剪辑,并将其添加到舞台上。既然每个movieclip都是用相同的名称“mc”创建的,为什么我只需将其放入一个数组中就可以处理单个电影剪辑?就像,鲍尔斯[0]将是我的第一部电影。为什么我不能创建:

for...
var balls[n]:ball = new ball();
然后像对待
球[0]
球[1]
等那样对待每一个球。。。? 为什么我必须将它添加到数组中,或者为每个数组指定不同的名称,使其唯一

另外,如何从自动创建球的类中创建对象,然后在删除此对象时也删除球

我想了解电影唇在AS3中的确切含义


谢谢

我已经尽力解释并回答了你的问题。如果你觉得我的答案的第一部分是一个太多的审查,请坚持它,并阅读所有,这将有助于你了解它的其余部分。所有这些都在代码中,所以请通读注释

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.
要使用它,我们只需要像创建任何其他对象一样创建它,还需要将参数传递给它的构造函数。我们不需要传递参数,但是如果我们不希望那些默认的大小和颜色应用到我们的球上,那么我们最好用不同的来替换它们

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.
array=newarray();
对于(变量i:int=0;i<10;i++){
推(新球(15,0x0000FF));
addChild(数组[array.length-1]);
}
//创建10个半径为15且为蓝色的球。然后我们也制作它们
//可显示。
请,如果你的问题已经得到了回答,那么接受这个答案,也许可以投票表决,但如果没有,那么请回答一个更详细的问题,我可以更好地理解,然后更好地解释答案。谢谢,这边走

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


很简单,不是吗?

很难说清楚你到底在问什么。这个问题似乎也是几个问题的组合。当人们看代码示例等而不先花时间学习基本的编程概念时,我会想到这类问题。什么是范围?什么是局部变量?什么是推荐信?避免学习这些概念并不能节省时间。你最终会浪费很多时间/天/周去思考和挣扎。我回答了这个问题,但如果你想全身心投入学习这门语言,那么请接受这个。也可能有一个免费的PDF格式,但我不确定。你的余生要么是一个电枢程序员,要么花几个月的时间真正学习,成为一个伟大的程序员。这真的取决于你。我不同意西勒的观点,那本书是一本很好的参考书,但不是一个很好的学习工具。它根本不会教你编程。对于初学者来说,这是一本更好的书:我只是假设他之前有编程知识,如果没有的话,那么我参考的那本书很难从中学习编程。但是,一旦您熟悉了基本编程技术,那么我强烈推荐我参考的那本书,因为它充分解释了OOP的基本原理,这就是问题的真正含义以及Actionscript 3.0的全部内容。
var array[index]=value
不是有效的语法。删除前导的
var
关键字。Ooops。。。这就是当你复制粘贴。。。谢谢有没有一种方法可以让我直接在课堂上“添加孩子”呢?如果这是可能的,那么我可以从库中添加现有的movieclip吗?您可以使用addChild将库中的类的实例添加到您的ball类中,当然可以。但是为什么不直接从库中创建类呢。实际上,您可以从库中转到movieclip并更改其类代码,以便:1)您可以通过flash professional CS6绘制其外观2)更改其类,以便您可以控制其行为。如果你想要一个这样的例子,那么告诉我,我可以发布另一个问题来回答。但请多解释,这样我才能回答更多。
for...
balls[n] = new ball();
balls.push(new ball());