Actionscript 3 AS3从蓝图创建新实例
我正在学习如何上课,这相当复杂。我需要一个正确创建对象的示例。比如说我的图书馆里有一张电影唇的蓝图。它可以作为一个示例来创建许多具有名称的实例,这些名称可以进一步用作代码中的对象以更改其属性。所以假装我有一个蓝图“AppleBP”。我将其设置为Apple类(假设它创建了一个文件Apple.as?)。我需要一个代码来创建(例如,通过按下按钮)一个实例apple1,该实例将出现在屏幕上的一个随机位置(只是为了知道它与其他实例不同,我知道如何随机化位置)。然后制作Apple02、Apple03、Apple04和Apple05。如果很难将name设置为“Apple”+N,那么可以使用一个带有名称的数组,或者至少像(N=1){//code for Apple01 creation}然后N++这样选择它 所以现在我试过了Actionscript 3 AS3从蓝图创建新实例,actionscript-3,class,Actionscript 3,Class,我正在学习如何上课,这相当复杂。我需要一个正确创建对象的示例。比如说我的图书馆里有一张电影唇的蓝图。它可以作为一个示例来创建许多具有名称的实例,这些名称可以进一步用作代码中的对象以更改其属性。所以假装我有一个蓝图“AppleBP”。我将其设置为Apple类(假设它创建了一个文件Apple.as?)。我需要一个代码来创建(例如,通过按下按钮)一个实例apple1,该实例将出现在屏幕上的一个随机位置(只是为了知道它与其他实例不同,我知道如何随机化位置)。然后制作Apple02、Apple03、App
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蓝图是不够的,那么我还缺少什么呢?如何为每个新实例选择新名称,使其具有变量字符串而不是纯名称?(这样我就可以根据顺序将名称更改为text+number)
加:
下面是一个例子:
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) ;
}
}
}
- 此外,所有变量的名称必须以小写字母开头。myVariable
- 类的名称必须实现CAMELCASECASE类
- 常量的名称只包含大字母和下划线。MY_常数
- 下面是一个示例:
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) ;
}
}
}
- 此外,所有变量的名称必须以小写字母开头。myVariable
- 类的名称必须实现CAMELCASECASE类
- 常量的名称只包含大字母和下划线。MY_常数
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) ;
}
}
}
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() ;
}
}
}