Hello folks,
I have the following problem:
I’m trying to create an enemy spaceship that has a simple AI. The basic idea is that the spaceship flies from one point to another. These points
are created on a random basis, i.e. everytime the spaceship reaches a point a new one will be created randomly and the ship is flying to it (etc., etc.)
Problem is that it doesn’t really work because after the ship flies around for some time it suddenly stops to move. It’s a matter of time. Sometimes the problem
occurs after 5 seconds then again the spaceships flies around for 2 - 3 minutes and stops the movement immediately.
Currently I have no clue where the problem is. I already checked the distance function but so far it seems to be okay. Can anyone help me, please?
Thank you very much.
This is the code (just an excerpt):
private function update(timeDifference:Number = 0):void
{
var step:Number = (timeDifference / 1000) * timeBasedUpdateModifier;
for each (tempEnemy in enemyManager.enemies)
{
tempEnemy.update(step);
switch (tempEnemy._type)
{
case 1:
tempEnemy.dX = tempEnemy.x - tempEnemy.destinationX;
tempEnemy.dY = tempEnemy.y - tempEnemy.destinationY;
tempEnemy.rotateTo = toDegrees(getRadians(tempEnemy.dX, tempEnemy.dY));
if(tempEnemy.frame < 0) tempEnemy.frame += 360;
if(tempEnemy.frame > 359) tempEnemy.frame -= 360;
tempEnemy.trueRotation = (tempEnemy.rotateTo - tempEnemy.frame) / 50;
tempEnemy.updateFrame(tempEnemy.trueRotation);
tempEnemy.vX += (tempEnemy.destinationX - tempEnemy.x) / tempEnemy._speed;
tempEnemy.vY += (tempEnemy.destinationY - tempEnemy.y) / tempEnemy._speed;
tempEnemy.vX *= tempEnemy.decay;
tempEnemy.vY *= tempEnemy.decay;
tempEnemy.nextX += tempEnemy.vX;
tempEnemy.nextY += tempEnemy.vY;
if (getDistance(tempEnemy.x, tempEnemy.y, tempEnemy.destinationX, tempEnemy.destinationY) < 50)
{
getRandomDestination();
}
break;
}
}
}
private function getRandomDestination():int
{
tempEnemy.oldDestX = tempEnemy.destinationX;
tempEnemy.oldDestY = tempEnemy.destinationY;
if (tempEnemy.oldDestX <= 660) {
tempEnemy.destinationX = Math.random() * (tempEnemy.maxX - tempEnemy.oldDestX)
+ tempEnemy.oldDestX + 3 * tempEnemy.bitmapSize;
} else {
tempEnemy.destinationX = Math.random() * (tempEnemy.minX + tempEnemy.oldDestX)
- 3 * tempEnemy.bitmapSize;
}
if (tempEnemy.oldDestY <= 500) {
tempEnemy.destinationY = Math.random() * (tempEnemy.maxY - tempEnemy.oldDestY)
+ tempEnemy.oldDestY + 3 * tempEnemy.bitmapSize;
} else {
tempEnemy.destinationY = Math.random() * (tempEnemy.minY + tempEnemy.oldDestY)
- 3 * tempEnemy.bitmapSize;
}
return(int(tempEnemy.destinationX), int(tempEnemy.destinationY));
}
private function toDegrees(radians:Number):Number
{
degrees = Math.floor(radians * 180 / Math.PI);
return degrees;
}
public function getRadians(deltaX:Number, deltaY:Number):Number
{
tempEnemy.radian = Math.atan2(deltaY, deltaX);
if (deltaY < 0)
{
tempEnemy.radian += (2 * Math.PI);
}
return tempEnemy.radian;
}
private function getDistance(distXOne:Number, distYOne:Number, distXTwo:Number, distYTwo:Number):Number {
distX = distXTwo - distXOne;
distY = distYTwo - distYOne;
dist = Math.sqrt(distX * distX + distY * distY);
return(dist);
}