我已尽力解释和回答您的问题。如果您觉得我的回答的第一部分过于评论,请坚持阅读并阅读,它将帮助您理解其余部分。所有这些都在代码中 - 所以请仔细阅读评论。
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.
请,如果您的问题已得到解答,请接受此答案并可能投票,但如果没有,请回答更详细的问题,我可以更好地理解并更好地解释答案。谢谢