I have a movie clip called food which I put in an array called foodArray. It has its own class (named Mover) which makes each food move to the right. when it reaches certain point, the food will vanish.
Here is the code to make the food vanish I put in the Main class. I put this on Enter Frame.
var l:int = foodArray.length - 1;
while ((l > -1))
{
if (foodArray[l].x > 1020)
{
removeChild(foodArray[l]);
foodArray.splice(l,1);
}
l = l - 1;
}
And this is the Mover class
public function Mover(inputMC:MovieClip, xV:Number)
{
this.insMC = inputMC;
this.xVel = xV;
}
public function startMove():void
{
this.insMC.addEventListener(Event.ENTER_FRAME, this.updatePos);
}
protected function updatePos(e: Event):void
{
this.insMC.x += this.xVel;
}
public function stopMove():void
{
this.insMC.removeEventListener(Event.ENTER_FRAME, this.updatePos);
}
The food is vanishing just fine, but there are some foods that just keep going even after the point where it should vanish. I'm guessing the problem is with the array length, but I can't be sure. Help will be greatly appreciated. :)
This is where I make the first eleven food. So I have a movieclip called Food which has six different pictures in each timeline. I want to make a row of random food.
function showFood()
{
const ROW = 11;
for (foodSeq = 0; foodSeq < ROW; foodSeq++)
{
randomFood = Math.ceil(Math.random() * 6);
food = new Food();
food.gotoAndStop(randomFood);
food.x = 1010 - (84 * (foodSeq % ROW));
food.y = 675;
addChild(food);
foodArray.push(food);
food.sequence = foodSeq;
food.code = randomFood;
food.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
//mover
mover = new Mover(food,XVEL);
moverArr.push(mover);
mover.startMove();
}
addFood();
}
And here's where I add one food at a time
function addFood()
{
randomFood = Math.ceil(Math.random() * 6);
food = new Food();
food.gotoAndStop(randomFood);
food.x = 76;
food.y = 675;
addChild(food);
foodArray.push(food);
food.sequece = foodSeq;
food.code = randomFood;
food.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
foodSeq++;
//mover
mover = new Mover(food,XVEL);
moverArr.push(mover);
mover.startMove();
//timer
timerFood.reset();
timerFood.addEventListener(TimerEvent.TIMER, timerForFood);
timerFood.start();
}
This is the timer which triggers the addFood function
function timerForFood(t: TimerEvent)
{
if (timerFood.currentCount > 2)
{
addFood();
}
}