Skip to main content
tropicanaclock
Known Participant
August 6, 2012
Question

Help: Random code selecting removed array item.

  • August 6, 2012
  • 1 reply
  • 544 views

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","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"];

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 ==theItem){

                              thearray .splice(i,1);

                              break;

                    }

          }

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

 

}

any help would be appericated

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
August 6, 2012

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

}