1

我正在学习课程的运作方式,这非常复杂。我需要一个正确创建对象的示例。假设我的库中有一个 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();

    }
}
}
4

1 回答 1

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() ;
    }
}
}
  • 此外,所有变量的名称必须以小写字母开头。我的变量
  • 类名必须实现 CamelCaseClass
  • 常量名只包含大写字母和下划线。MY_CONSTANT
于 2013-02-17T14:29:08.347 回答