简体   繁体   English

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

[英]How to create multiple instances of the same class in as3?

Noob question. 新手问题。 Ok, so I'm trying to create two instances of my notes (it's an imported pic) class at separate x and y coordinates, then I want both of them to move right. 好的,所以我试图在单独的x和y坐标上创建笔记的两个实例(这是一个导入的图片),然后我希望它们都向右移动。 Right now I programmed a loop, and the loop works OK, but it only keeps the last instance that was created. 现在,我编写了一个循环,该循环可以正常工作,但它只保留最后一个创建的实例。 Here's my code. 这是我的代码。 I really appreciate any help that anyone can give. 我真的很感谢任何人都能提供的帮助。 Thanks! 谢谢!

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);

您已经创建了4个音符,但是它们的位置是同一点(0,0)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM