Removing items from Arrays

I have created a simple array that attaches the mc 0 to 23 from the library randomly and used Array.splice(); to remove an element from the array once it has been displayed to the stage but it doesn’t work.

I think the problem is in my for and splice methods

but I dont know what it is, something simple Im sure.

for (i = 0 ; i <= picArray.length; i++)

picArray.splice(this.picArray[rndNum]);

can someone help me please

:m:


var picArray:Array = new Array();
picArray[0] = "bottle";
picArray[1] = "bucket";
picArray[2] = "carrierbag";
picArray[3] = "chair";
picArray[4] = "coat";
picArray[5] = "cushion";
picArray[6] = "envelope";
picArray[7] = "fork";
picArray[8] = "hat";
picArray[9] = "jug";
picArray[10] = "key";
picArray[11] = "kitchenroll"; 
picArray[12] = "lunchbox";
picArray[13] = "mirror";
picArray[14] = "newspaper";
picArray[15] = "pan";
picArray[16] = "phonebook";
picArray[17] = "pictureframe"; 
picArray[18] = "spoon";
picArray[19] = "toyboat";
picArray[20] = "toydoll";
picArray[21] = "trophy";
picArray[22] = "trouser";
picArray[23] = "vase";

for (i = 0 ; i <= picArray.length; i++)
{
     rndNum = Math.floor(Math.random(23) * picArray.length);
     this.blank.attachMovie(this.picArray[rndNum], pic1, 0);
     trace(i + " : " + rndNum + " : " + picArray[rndNum]);
    
     i = 0;
     picArray.splice(this.picArray[rndNum]);
     

}
stop();

maybe like this?

var i =0
trace(picArray.length)
rndNum = Math.floor(Math.random()*picArray.length);
this.blank.attachMovie(picArray[rndNum], "pic"+i, i++);
picArray.splice(rndNum,1);
trace(picArray.length)

[ot]the new mod gets busy :smiley: [/ot]

thanks for the help

no still not removing attached MC from array when displayed to screen.

:book:
I can’t figure out what I am doing wrong.

trace

trace(picArray.length + " : " + rndNum + " : " + picArray[rndNum]);

output to code

i.e jug and bucket have been repeated

23 : 5 : envelope
23 : 8 : jug
23 : 8 : jug
23 : 9 : key
23 : 14 : pan
23 : 20 : trophy
23 : 0 : bucket
23 : 17 : spoon
23 : 2 : chair
23 : 18 : toyboat
23 : 19 : toydoll
23 : 21 : trouser
23 : 0 : bucket
23 : 13 : newspaper
23 : 19 : toydoll
23 : 0 : bucket
23 : 17 : spoon
23 : 8 : jug
23 : 11 : lunchbox
23 : 16 : pictureframe
23 : 9 : key
23 : 15 : phonebook
23 : 2 : chair
23 : 9 : key

copy/paste this into a new movie and see what you get in the output window

var picArray:Array = new Array();
picArray[0] = "bottle";
picArray[1] = "bucket";
picArray[2] = "carrierbag";
picArray[3] = "chair";
picArray[4] = "coat";
picArray[5] = "cushion";
picArray[6] = "envelope";
picArray[7] = "fork";
picArray[8] = "hat";
picArray[9] = "jug";
picArray[10] = "key";
picArray[11] = "kitchenroll";
picArray[12] = "lunchbox";
picArray[13] = "mirror";
picArray[14] = "newspaper";
picArray[15] = "pan";
picArray[16] = "phonebook";
picArray[17] = "pictureframe";
picArray[18] = "spoon";
picArray[19] = "toyboat";
picArray[20] = "toydoll";
picArray[21] = "trophy";
picArray[22] = "trouser";
picArray[23] = "vase";
var i = 0;
trace(picArray.length);
mi = setInterval(function () {
	rndNum = Math.floor(Math.random()*picArray.length);
	//this.blank.attachMovie(picArray[rndNum], "pic"+i, i++);
	trace("picArray.length= "+picArray.length+" picArray[rndNum] = "+picArray[rndNum]);
	picArray.splice(rndNum, 1);
	if (picArray.length == 0) {
		clearInterval(mi);
	}
}, 1000);

made a quick file with the attachMovie stuff

thanks stringy this array has been doing my head in, been going through flash mx 2004 actionscript book looking for answers…

so to attach Movie to stage all you will need is

this.blank.attachMovie(picArray[rndNum]);

didn’t see attachment will have a look at that thanks again

 blank.attachMovie(picArray[rndNum], picArray[rndNum], blank.getNextHighestDepth());

in the file i uploaded change the code to this -might be nearer to what you want

this.createEmptyMovieClip("blank", 1);
mi = setInterval(function () {
	rndNum = Math.floor(Math.random()*picArray.length);
	var clip = blank.attachMovie("pic", picArray[rndNum], blank.getNextHighestDepth());
	clip._x = random(750);
	clip._y = random(550);
	clip.myText.text = picArray[rndNum];
	trace("picArray.length= "+picArray.length+" picArray[rndNum] = "+picArray[rndNum]);
	picArray.splice(rndNum, 1);
	if (picArray.length == 0) {
		clearInterval(mi);
	}
}, 1000);

i was too lazy to put loads of clips in the library-there is only 1 with linkage “pic”

I modified the code and testing

var clip = _root.attachMovie(picArray[rndNum], “pic”+i, i++);

and

picArray.splice(picArray[rndNum]);