创建自定义 class 并链接到 Main class

Creating a custom class and linking to Main class

我几乎只想为我的单词列表创建一个单独的 class,而不是让我的主要 class 混乱。如果数组在我的 Main Class 中,一切正常。我的问题是我不确定我应该如何创建数组自定义 class 然后 link 到主 class。这是我目前所拥有的

package  {

public class RandomText  {


    public function RandomText() {
        var randomKill:Array = new Array("Goats",
                      "Bananas",
                      "Cows",
                      "a Printer",
                      "Pineapples",
                      "a Toothbrush")


    }

}

}

这是我的 Main Class 中的那个,但我不确定如何 link 数组 class 到这个。我一直收到一条错误消息,说 属性 在 RandomText 上找不到长度并且 Main/moveCell() 没有默认值。 非常感谢!

 for (var i:int = 0; i < group.length; i++)
             {
                 var cell:Cell = Cell(group[i]);
                 if (cell.hitTestObject(island))
                 {
                     cell.parent.removeChild(cell);
                     group.splice(i,1);

                     score++;
                     //trace("hit me");

                     var randomText = new RandomText();
                      randomIndex =  Math.random () * randomText.length;
                     txtWordDisplay.text = "Killed by " + randomText[randomIndex]

                 }


             }

您的变量 'randomKill' 卡在您的构造函数范围内。 在下面的示例中,您需要将 randomKill 作为对象的属性:

package  {

public class RandomText  {   
    public var randomKill:Array = new Array("Goats",
                  "Bananas",
                  "Cows",
                  "a Printer",
                  "Pineapples",
                  "a Toothbrush");
     }
}

// -- You access 'randomKill' like this: 
var randomText = new RandomText();
randomIndex =  Math.random () * randomText.randomKill.length;
txtWordDisplay.text = "Killed by " + randomText.randomKill[randomIndex];