将对象放入as3中的数组中

时间:2020-12-15 21:38:18

So I'm trying to create a row of ten buttons using a for loop that create a new button every time it runs, changing the x value each time. However ever each time a button is created i want it to be put into a specific array so i can refer to a specific button later on. However I'm not sure how to put objects into arrays. Is it possible to do this? This is the code I have so far:

我尝试用for循环创建一行10个按钮每次运行时创建一个新按钮,每次都改变x值。然而,每次创建一个按钮时,我都希望它被放入一个特定的数组中,以便以后可以引用一个特定的按钮。但是我不知道如何将对象放入数组中。有可能这样做吗?这是我迄今为止的代码:

package {

包{

import flash.display.MovieClip;
import flash.events.MouseEvent;

public class backgound extends MovieClip {

    var btnx = 30;
    var btny= 20;
    var row1:Array = [];

    public function backgound() {
        // constructor code
        var continueBtn:Button;
        for(var i=0; i < 10; i++)
        {
            continueBtn = new Button();

            continueBtn.x = btnx;
            continueBtn.y = 100;
            continueBtn.width = 30;
            continueBtn.height = 20;
            continueBtn.border = true;
            continueBtn.visible = true;
            continueBtn.label = "Continue";
            addChild(continueBtn);
            btnx += 30;
        }



    }
}

}

}

1 个解决方案

#1


0  

in your loop:

在你的循环:

myArray.push(continueBtn);

or in your loop:

或者你的循环:

continueBtn =myArray[i]= new Button();  

or many other ways.

或其他方式。

now you can access your buttons:

现在您可以访问您的按钮:

 myArray[3]// gets the 4th item in your array

I just wonder, is this ALL you want?

我只是好奇,这就是你想要的吗?

I  H☺P E  this helps !

这个帮助我H☺P E !

#1


0  

in your loop:

在你的循环:

myArray.push(continueBtn);

or in your loop:

或者你的循环:

continueBtn =myArray[i]= new Button();  

or many other ways.

或其他方式。

now you can access your buttons:

现在您可以访问您的按钮:

 myArray[3]// gets the 4th item in your array

I just wonder, is this ALL you want?

我只是好奇,这就是你想要的吗?

I  H☺P E  this helps !

这个帮助我H☺P E !