Nathan
Nathan

Reputation: 536

How to assign value to Numbers in Array?

Hey everyone so I have an Array private var frames:Array; which I give value and initiate in my constructor function like so frames = [2, 3, 4, 5, 6, 7, 8];

Now I am trying to give a string value to each number in the array for my hitTest Function. I was thinking something on the lines of a for loop and giving them values there but having some issues here is what I have so far:

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";
           }
        }

this doesn't work at all. I know I Am missing something crucial. Please any help would be appreciated thanks!

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

I have my array of Movie clips like so aClockArray = [playScreen.wire_5, playScreen.wire_6, playScreen.wire_7, playScreen.wire_8];

in my last post I finally figure out how to randomize the array with no repeat like so:

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

and above in my frames for loop is where I assign the values to the numbers in the frames array for my hitTest which I try to accomplish like so:

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

I cant seem to figure this out lost in code haha. I probably have it set up really badly.

basically the game is I have 7 colored wires on the stage that the user can drag and place inside the correct color slot. It was working fine until I had added the var randomFrame:uint = frames.splice(Math.floor(Math.random() * frames.length), 1); I had to change things around. It was working fine when my original code was like so:

//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";
           }
        }

Upvotes: 0

Views: 52

Answers (1)

duggulous
duggulous

Reputation: 2737

I'm not sure exactly what issue you're seeing, and it's hard to know exactly what's going on without seeing the entire code, but I'll take a stab at it...

rather than currentWire.sRed = "RED";

use a single variable to represent the current color, like so:

currentWire.color = "RED";

then..

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

Upvotes: 1

Related Questions