我正在学习课程是如何工作的,这很复杂。我需要一个正确的对象创建的例子。假设我的库中有一个MovieClip蓝图。它可以用作创建许多实例的示例,这些实例的名称可以进一步用作代码中的对象以更改其属性。所以假装我有一个蓝图“AppleBP”。我把它设置为Apple类(假设它创建了一个Apple.as文件?)。我需要一个代码来创建(通过按下按钮)一个实例Apple01,它将出现在屏幕上的一个随机位置(只是为了知道它与其他人不同,我知道如何随机化位置)。然后制作Apple02,Apple03,Apple04和Apple05。如果很难将名称设置为“Apple”+ N,那么可以坚持使用带有名称的数组,或者至少选择if(N = 1){// Apple01创建代码}然后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
Apple没有出现,文字没有。那么,如果创建一个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);
}
}
}
}
Apple.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();
}
}
}
答案 0 :(得分:1)
以下是评论示例:
package
{
import flash.display.Scene;
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Main extends Sprite
{
public function Main():void {
var apples:Array = new Array() ; // Declare and array
for (var i:int = 0 ; i < 10 ; i++) { //Let's make 10 apples
var apple:Apple = new Apple() ; //Create an apple
apple.x = Math.ceil(Math.random() * stage.stageWidth) ; //Assign random x
apple.y = Math.ceil(Math.random() * stage.stageHeight) ; //Assign random y
stage.addChild(apple) ; //Add it to stage, so we can see them
apples.push(apple) ; //Push into array
}
//EDITED
var draggableApple:Apple = apples[0] ;
draggableApple.addEventListener(MouseEvent.MOUSE_DOWN, draggableApple.onMouseDown) ;
draggableApple.addEventListener(MouseEvent.MOUSE_UP, draggableApple.onMouseUp) ;
}
}
}
和Apple.as
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Apple extends MovieClip {
public function Apple() {
this.graphics.lineStyle(2) ;
this.graphics.beginFill(0x00FF40) ;
this.graphics.drawCircle(0, 0, 20) ;
this.graphics.endFill() ;
//addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown) ;
//addEventListener(MouseEvent.MOUSE_UP, onMouseUp) ;
}
public function onMouseDown(event:MouseEvent):void {
this.startDrag() ;
}
public function onMouseUp(event:MouseEvent):void {
this.stopDrag() ;
}
}
}