如何在as3中创建同一个类的多个实例?

时间:2013-09-05 01:03:32

标签: arrays actionscript-3

Noob问题。好吧,所以我试图在不同的x和y坐标处创建我的笔记的两个实例(它是一个导入的图片),然后我希望它们都向右移动。现在我编写了一个循环,循环工作正常,但它只保留最后创建的实例。这是我的代码。我非常感谢任何人都可以给予的任何帮助。谢谢!

package 
{
import flash.display.Bitmap;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.utils.getDefinitionByName;

[Frame(factoryClass="Preloader")]
public class Main extends Sprite 
{
private var speed:int = 8;
[Embed(source="../lib/Dodgethis.jpg")]
public var Notes:Class;

public var numnotes:Number;

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point
        stage.addEventListener(KeyboardEvent.KEY_DOWN, testevent);

        }

    private function testevent(e:Event = null):void {
        trace("testevent has run");
        appear(350, 250);
        //ap2(150, 150)
        //addEventListener(Event.ENTER_FRAME, loop, false, 0, true);    
    }

    private function appear(x:Number, y:Number) {
        var arr1:Array = new Array;
        numnotes = 4;

        for (var i = 0; i < numnotes; i++)
        {
            trace (i);
            var nbm:Bitmap = new Notes;

            if (i == 0) {
                this.x = 400;
                this.y = 400;
                addChild(nbm);
                trace ("1 should be different");
            } else {
                trace ("this is working");
                this.x = 150;
                this.y = 150;
                addChild(nbm);
                arr1.push(nbm);

1 个答案:

答案 0 :(得分:0)

您创建了4个Notes,但它们的位置是相同的点(0,0)。