首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为数组中的数字赋值?

如何为数组中的数字赋值?
EN

Stack Overflow用户
提问于 2015-09-23 02:26:57
回答 1查看 47关注 0票数 0

大家好,我有一个数组private var frames:Array;,我在构造函数中给它赋值并初始化,就像这样frames = [2, 3, 4, 5, 6, 7, 8];

现在,我尝试为hitTest函数的数组中的每个数字赋予一个字符串值。我在想一些关于for循环的东西,并在那里给它们赋值,但到目前为止,我遇到了一些问题:

代码语言:javascript
复制
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";
           }
        }

这根本不起作用。我知道我错过了一些至关重要的东西。如有任何帮助,不胜感激,谢谢!

*更新*

我有像so aClockArray = [playScreen.wire_5, playScreen.wire_6, playScreen.wire_7, playScreen.wire_8];这样的电影剪辑数组

在我的上一篇文章中,我终于想出了如何在不重复的情况下随机化这个数组,如下所示:

代码语言:javascript
复制
//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数组中的数字赋值的地方,我试图像这样完成:

代码语言:javascript
复制
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);,我必须改变周围的东西。当我的原始代码是这样的时候,它工作得很好:

代码语言:javascript
复制
//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";
           }
        }
EN

回答 1

Stack Overflow用户

发布于 2015-09-23 04:40:19

我不确定你到底看到了什么问题,如果不看完整的代码就很难知道到底发生了什么,但我会尝试一下……

而不是currentWire.sRed = "RED";

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

currentWire.color = "RED";

然后..。

代码语言:javascript
复制
if (redCopper.hitTestObject(currentWires) && currentWires.color== "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.color== "BLUE")
{
    //trace("HIT_BLUE");
    hasBlueWire = false;
    blueCopper.removeEventListener(MouseEvent.MOUSE_DOWN, blueWireFunction);
    blueWire.removeEventListener(MouseEvent.MOUSE_UP, blueWireFunction);
    trace("HIT");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32724228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档