如何为Array中的Numbers赋值?

时间:2015-09-22 18:26:57

标签: arrays actionscript-3 for-loop

嘿大家所以我有一个数组private var frames:Array;,我给它赋值并在我的构造函数中启动,如frames = [2, 3, 4, 5, 6, 7, 8];

现在我试图为我的hitTest函数给数组中的每个数字赋一个字符串值。我正在思考一个for循环的路线并给它们值,但是到目前为止我遇到了一些问题:

for (var i:int = 0; i < frames.length; i++)
        {
           var currentFrameNumber = frames[i];

            //assign values to numbers in array
           if (currentFrameNumber == 2)
           {
               trace("2_RED");
               currentWires.sRed = "RED";
           }
           if (currentFrameNumber == 3)
           {
               trace("GREEN");
               currentWires.sGreen = "GREEN";
           }
           if (currentFrameNumber == 4)
           {
               trace("BLUE");
               currentWires.sBlue = "BLUE";
           }
           if (currentFrameNumber == 5)
           {
               trace("YELLOW");
               currentWires.sYellow = "YELLOW";
           }
           if (currentFrameNumber == 6)
           {
               trace("WHITE");
               currentWires.sWhite = "WHITE";
           }
           if (currentFrameNumber == 7)
           {
               trace("PURPLE");
               currentWires.sPurple = "PURPLE";
           }
           if (currentFrameNumber == 8)
           {
               trace("BLACK");
               currentWires.sBlack = "BLACK";
           }
        }

这根本不起作用。我知道我错过了至关重要的事情。请任何帮助,谢谢!

**************** UPDATE ****************

我有像aClockArray = [playScreen.wire_5, playScreen.wire_6, playScreen.wire_7, playScreen.wire_8];

这样的电影片段

在我的上一篇文章中,我终于弄明白了如何随机化数组而不重复:

//Loop through wires and make them randomn generate color
        for (var i:int = 0; i < aClockArray.length; i++)
        {
           var currentWires = aClockArray[i];
           var randomFrame:uint = frames.splice(Math.floor(Math.random() * frames.length), 1);


           //nWire = randomNumber(2, 8);

           currentWires.gotoAndStop(randomFrame);
           }
我在frames for循环中的

及以上是我为hitTest分配frames数组中的数字的值,我试图这样做:

private function wireHitTestFunction():void 
    {
        //Loop through wires and make them randomn generate color
        for (var i:int = 0; i < aClockArray.length; i++)
        {
           var currentWires = aClockArray[i];

           if (redCopper.hitTestObject(currentWires) && currentWires.sRed == "RED")
           {
               //trace("HIT_ RED");
               hasRedWire = false;
               redCopper.removeEventListener(MouseEvent.MOUSE_DOWN, redWireFunction);
               redWire.removeEventListener(MouseEvent.MOUSE_UP, redWireFunction);
                trace("HIT");

           }
           if (blueCopper.hitTestObject(currentWires) && currentWires.sBlue == "BLUE")
           {
               //trace("HIT_BLUE");
               hasBlueWire = false;
               blueCopper.removeEventListener(MouseEvent.MOUSE_DOWN, blueWireFunction);
               blueWire.removeEventListener(MouseEvent.MOUSE_UP, blueWireFunction);
                trace("HIT");
           }

我似乎无法弄清楚代码丢失了哈哈。我的设置可能非常糟糕。

基本上游戏是我在舞台上有7条彩色线,用户可以拖动并放置在正确的颜色槽内。它工作正常,直到我添加了var randomFrame:uint = frames.splice(Math.floor(Math.random() * frames.length), 1);我不得不改变一切。当我的原始代码如此时,它工作正常:

//Loop through wires and make them randomn generate color
        for (var i:int = 0; i < aClockArray.length; i++)
        {
           var currentWires = aClockArray[i];
           //var randomFrame:uint = frames.splice(Math.floor(Math.random() * frames.length), 1);


           nWire = randomNumber(2, 8);

           currentWires.gotoAndStop(nWire);

           //If any of the Wires lands on 2,3,etc.. Assign Color for Hit Test
           if (nWire == 2)
           {
               trace("2_RED");
               currentWires.sRed = "RED";
           }
           if (nWire == 3)
           {
               trace("GREEN");
               currentWires.sGreen = "GREEN";
           }
           if (nWire == 4)
           {
               trace("BLUE");
               currentWires.sBlue = "BLUE";
           }
           if (nWire == 5)
           {
               trace("YELLOW");
               currentWires.sYellow = "YELLOW";
           }
           if (nWire == 6)
           {
               trace("WHITE");
               currentWires.sWhite = "WHITE";
           }
           if (nWire == 7)
           {
               trace("PURPLE");
               currentWires.sPurple = "PURPLE";
           }
           if (nWire == 8)
           {
               trace("BLACK");
               currentWires.sBlack = "BLACK";
           }
        }

1 个答案:

答案 0 :(得分:1)

我不确定你到底发生了什么问题,而且很难在不看完整个代码的情况下确切地知道发生了什么,但我会对它进行抨击......

而不是bottom Pure bottom Free bottom

使用单个变量来表示当前颜色,如下所示:

implicit def javaFieldtoScalaField(jField: Field): Field

然后..

currentWire.sRed = "RED";