EDIT: EVERY ANSWER BELOW ARE WORKING, THANKS FOR HELPING ME!
I'm currently learn about splicing an array in as3. So here's my code:
//import classes
import flash.utils.Timer;
import flash.events.*;
//variables
var Arr:Array=new Array();
var num:Number=0;
//set a timer and set timer limit of 10 times
var timer:Timer=new Timer(1000,10);
//add a listener to our timer object
timer.addEventListener(TimerEvent.TIMER, tick);
timer.addEventListener(TimerEvent.TIMER_COMPLETE,tock);
//tick function
function tick(e:TimerEvent):void{
//i add an element each time the timer 'ticks'
Arr.push(['index'+num]);
num++;
}
//tock function
function tock(e:TimerEvent):void{
trace('array elements :'+Arr);//traces Arr elemnts
for(var i:int=0;i<Arr.length;i++){
Arr.splice(i,1);// i've tried Arr.splice(0,1), but neither working
trace('elemnts left : '+Arr);
}
I dont really understand the problem, but here's the result:
1.not every elements in Arr array have been removed 2.the maximum Arr's length is ten before spliced.BUT in the loop, its only splicing less than ten times, which it causes the problem above
anybody have an idea for this? Please help me out
Arr.length
which changes every time the loop iterates. The first time it iterates it checks ifi
(currently 0) is smaller than 10. Then you remove an item and incrementi
to 1. On the next iteration it checks ifi
(now 1) is smaller than 9. See what I'm getting at? – Comyns