0

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

1 Answers1

1

I would make a few changes. Firstly, you could remove the event listeners from Mover and call the update method from your while loop instead - so you don't have to have multiple enter frame listeners (since I can't see you removing any listeners, I'm assuming you have a lot of memory leaking).

I would also skip that backwards loop, since it's just ugly and makes the code harder to understand :) While it's a tiny bit faster, that wont matter unless you have many thousands of items in the array.

for (var i:int = 0; i<foodArray.length; i++) {
    var food:Mover = foodArray[i];
    food.updatePos(); // Remove the parameters from the updatePos method, since you're not using it any more.
    if (food.x > 1020) {
        foodArray.splice(i,1);
        i--;
    }
 }

That being said, I can't see anything obviously wrong with the code (unless there is something weird in your backwards loop), so please post some more code.

Jonatan Hedborg
  • 4,382
  • 21
  • 30