Skip navigation
Currently Being Moderated

Help: Random code selecting removed array item.

Aug 6, 2012 3:02 PM

Hello struggling with this bit of code.

What im trying to do is whenever pickCircuit function is called, randomly color a button and remove it from the array so it cant be "selected" again.

 

The problem: my code is still selecting deleted items off the array.

 

 

 

 

 

//array

var brkArr:Array = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","1 6","17","18","19","20","21","22","23","24","25","26","27","28","29","3 0"];

trace(brkArr);

 

 

function pickCircuit (){

 

 

//randomizer

var brkRa:Number = Math.ceil(Math.random()*brkArr.length);

trace(brkArr.length);

 

//change color

 

var brkPik:ColorTransform = new ColorTransform();

brkPik.blueOffset = 150;

this[("brk" + brkRa)].transform.colorTransform = brkPik;

 

removeItemArray(brkArr,brkRa);

 

}

 

// Code removes items from array

 

function removeItemArray(thearray , theItem):void{

          trace("Remove "+theItem+" from "+thearray );

          for(var i:int=0; i<thearray .length;i++){

                    if(thearray [i]==theItem){

                              thearray .splice(i,1);

                              break;

                    }

          }

          trace(" array is now - "+thearray );

 

}

 

 

any help would be appericated

 
Replies
  • kglad
    62,194 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 6, 2012 3:54 PM   in reply to tropicanaclock

    use:

     

     

    //array

    var brkArr:Array = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","1 6","17","18","19","20","21","22","23","24","25","26","27","28","29"," 3 0"];

    trace(brkArr);

     

     

    function pickCircuit (){

     

     

    //randomizer

    var brkRa:Number = Math.floor(Math.random()*brkArr.length);

    trace(brkArr.length);

     

    //change color

     

    var brkPik:ColorTransform = new ColorTransform();

    brkPik.blueOffset = 150;

    this[("brk" + brkRa)].transform.colorTransform = brkPik;

     

    removeItemArray(brkArr,brkRa);

     

    }

     

    // Code removes items from array

     

    function removeItemArray(thearray , theItem):void{

              trace("Remove "+theItem+" from "+thearray );

              thearray .splice(theItem,1);

              trace(" array is now - "+thearray );

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points