我有一个名为 food 的电影剪辑,我将它放入一个名为 foodArray 的数组中。它有自己的类(名为 Mover),它使每个食物向右移动。当它达到一定程度时,食物就会消失。
这是我放在 Main 类中的让食物消失的代码。我把它放在 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;
}
这是 Mover 类
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);
}
食物消失得很好,但有些食物即使在它应该消失的点之后仍然继续存在。我猜问题出在数组长度上,但我不能确定。帮助将不胜感激。:)
这是我制作前十一种食物的地方。所以我有一个名为 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();
}
这是我一次添加一种食物的地方
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();
}
这是触发 addFood 函数的计时器
function timerForFood(t: TimerEvent)
{
if (timerFood.currentCount > 2)
{
addFood();
}
}