"game" loop in flash virtual pond
i am having trouble on designing the "game" loop in my flash as3 virtual pond currently i have the following enter_frame loop. in my "pond" parent container, there will be arbitrary amount of "fish" objects and "food pellet" objects. currently in my loop function when two or more food is placed开发者_如何学运维 in near distance of the fish , the fish will not move does anyone know why this happens?
the below code is in my "Fish" class, updatePosition() merely tells the fish to swim around like a fish.
public function loop(e:Event):void
{
//getDistance(this.x - i.x, this.y - i.y)
if(foodDroppedArray.length > 0)
{
for each (var i:Food in foodDroppedArray)
{
if (getDistance(this.x - i.x, this.y - i.y) < 100)
{
this.moveToFood(i);
}else {
updatePosition();
}
}
}else
{
updatePosition();
}
}
What you have here loops through the food array and for each item in that array it will either call 'moveToFood' or 'updatePosition' based on a distance condition. Presumably these two functions both control motion, and so they may be working against each other, in which case you would only see the last result of the loop being output.
You probably want to break the loop when a suitable bit of food is discovered, and only call updatePosition in the event of no food items matching your condition.
if(foodDroppedArray.length > 0)
{
for each (var i:Food in foodDroppedArray)
{
if (getDistance(this.x - i.x, this.y - i.y) < 100)
{
this.moveToFood(i);
return;
}
}
//this only calls when no food matches the condition
updatePosition();
} else {
updatePosition();
}
精彩评论