我正在学习课程的运作方式,这非常复杂。我需要一个正确创建对象的示例。假设我的库中有一个 MovieClip 蓝图。它可以用作创建许多实例的示例,这些实例的名称可以进一步用作代码中的对象以更改其属性。所以假装我有一个蓝图“AppleBP”。我将它设置为 Apple 类(假设它创建了一个文件 Apple.as?)。我需要一个代码来创建(例如通过按下按钮)一个实例 Apple01,它会出现在屏幕上的随机位置(只是为了知道它与其他位置不同,我知道如何随机化位置)。然后制作 Apple02、Apple03、Apple04 和 Apple05。如果很难将名称设置为“Apple”+ N,则它'
所以现在我尝试了
var Apple01:Apple = new Apple();
Apple01.visible = true; //just in case I can't see it
Apple01.x = 100;
Apple01.y = 100;
TextField01.text = "blahblah"; //to see if code actually works
苹果不会出现,文字会出现。那么,如果创建一个作为 MovieClip 蓝图的新类还不够,我还缺少什么?以及如何为每个新实例选择新名称,以便它具有变量 String 而不是纯名称?(这样我就可以根据它的顺序将名称更改为文本+数字)
添加:
package {
import flash.display.Scene;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.events.*;
import flash.display.*;
public class MyFirstClass extends MovieClip{
//var N:int;
var n:int;
var existingApples:int = 0;
var apples:Array = new Array();
public function MyFirstClass():void{
MakeNewApple();
MakeNewApple();
MakeNewApple();
MakeNewApple();
MakeNewApple();
//apple4.x = 5;
//apple4.y = 5;
var draggableApple:Apple = apples[0] ;
draggableApple.addEventListener(MouseEvent.MOUSE_DOWN, draggableApple.onMouseDown) ;
draggableApple.addEventListener(MouseEvent.MOUSE_UP, draggableApple.onMouseUp) ;
info01.text = "Did it";
}
public function MakeNewApple():void{
if(existingApples < 5){
n = existingApples;
var apple:Apple = new Apple();
stage.addChild(apple);
apple.x = (Math.random()*600+100);
apple.y = (Math.random()*400+100);
apple.name = "apple" + n;
apples.push(apple.name);
existingApples++;
trace(existingApples);
trace(apples);
}
}
}
}
苹果.as:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.*;
import flash.display.*;
public class Apple extends MovieClip{
public function Apple() {
}
public function onMouseDown(e:MouseEvent):void{
this.startDrag();
}
public function onMouseUp(e:MouseEvent):void{
this.stopDrag();
}
}
}