ActionScript 3 enemy tracking logic issues

I’m working on a naval combat game where the player controls a warship and needs to fight against computer-controlled vessels. The enemy ships need basic AI behavior to follow the player’s position and engage in combat.

My problem is figuring out how to make enemy vessels chase the player ship effectively. I’ve tried several approaches for movement logic but none seem to work properly.

Here’s my current attempt at enemy movement code (player_ship is the player object, enemy_vessel is the AI ship):

var movementTimer:Timer = new Timer(15);
movementTimer.addEventListener("timer", updateEnemyPosition);

function updateEnemyPosition(e:TimerEvent):void{
    movementTimer.start();
    
    if(((enemy_vessel.x + 25.5) < player_ship.x) && (enemy_vessel.x < player_ship.x) && ((enemy_vessel.y + 180.2) < player_ship.y) && (enemy_vessel.y < player_ship.y)){
        enemy_vessel.x += 1.5;
        enemy_vessel.y += 1.5;
    }
    else if(((enemy_vessel.x + 25.5) < player_ship.x) && (enemy_vessel.x < player_ship.x) && (enemy_vessel.y > (player_ship.y + 180.2)) && ((enemy_vessel + 180.2) > (player_ship.y + 180.2))){
        enemy_vessel.x += 1.5;
        enemy_vessel.y -= 1.5;
    }
    else if((enemy_vessel.x > (player_ship.x + 25.5)) && ((enemy_vessel.x + 25.5) > (player_ship.x + 25.5)) && ((enemy_vessel.y + 180.2) < player_ship.y) && (enemy_vessel.y < player_ship.y)){
        enemy_vessel.x -= 1.5;
        enemy_vessel.y += 1.5;
    }
    else if((enemy_vessel.x > (player_ship.x + 25.5)) && ((enemy_vessel.x + 25.5) > (player_ship.x + 25.5)) && (enemy_vessel.y > (player_ship.y + 180.2)) && ((enemy_vessel + 180.2) > (player_ship.y + 180.2))){
        enemy_vessel.x -= 1.5;
        enemy_vessel.y -= 1.5;
    }
    else if((enemy_vessel.x > player_ship.x) && ((enemy_vessel.x + 25.5) < (player_ship.x + 25.5)) && ((enemy_vessel.y + 180.2) < player_ship.y) && (enemy_vessel.y < player_ship.y)){
        enemy_vessel.y += 1.5;
    }
    else if((enemy_vessel.x > player_ship.x) && ((enemy_vessel.x + 25.5) < (player_ship.x + 25.5)) && (enemy_vessel.y > (player_ship.y + 180.2)) && ((enemy_vessel.y + 180.2) > (player_ship.y + 180.2))){
        enemy_vessel.y -= 1.5;
    }
    else if((enemy_vessel.y > player_ship.y) && ((enemy_vessel.y + 180.2) < (player_ship.y + 180.2)) && ((enemy_vessel.x + 25.5) < player_ship.x) && (enemy_vessel.x < player_ship.x)){
        enemy_vessel.x += 1.5;
    }
    else if((enemy_vessel.y > player_ship.y) && ((enemy_vessel.y + 180.2) < (player_ship.y + 180.2)) && (enemy_vessel.x > (player_ship.x + 25.5)) && ((enemy_vessel.x + 25.5) > (player_ship.x + 25.5))){
        enemy_vessel.x += 1.5;
    }
}

I need help with these specific questions:

  1. What’s the right approach for making enemies follow the player smoothly?
  2. How can I implement enemy shooting mechanics?
  3. What’s the best way to detect when enemy ships collide with each other?

I really need working code examples since this project has a tight deadline. Thanks for any help you can provide.

Your tracking system is way too complicated with all those distance calculations. I hit the same wall building an RTS a few years ago. Ditch the conditional checks and just use basic Pythagorean theorem: var distance = Math.sqrt((player_ship.x - enemy_vessel.x) * (player_ship.x - enemy_vessel.x) + (player_ship.y - enemy_vessel.y) * (player_ship.y - enemy_vessel.y)). Normalize movement by dividing the x and y differences by this distance, then multiply by your speed. For shooting, I used a simple range check - when distance drops below your threshold, fire with a separate timer controlling the rate. Definitely pull that timer.start() from your event handler like others said - it’s killing performance. Handle collision between enemy ships with a nested loop using hitTestObject on all pairs, but add separation logic so they don’t stick together.

Your code’s a mess lol. Those random offset values (25.5, 180.2) are probably breaking everything. Ditch the crazy conditionals and use simple x/y distance checks instead. Try if(enemy_vessel.x < player_ship.x) enemy_vessel.x += speed - way cleaner than whatever this is supposed to do.

Your movement logic is way too complicated - that’s why tracking isn’t working. You’re checking tons of conditions when a simple vector approach would fix this. I had the same issue in my tower defense game. Just calculate the angle between enemy and player positions for smoother movement. Use var angle = Math.atan2(player_ship.y - enemy_vessel.y, player_ship.x - enemy_vessel.x) then Math.cos(angle) and Math.sin(angle) for your movement vectors. For shooting, I used basic distance checks - enemy gets in range, start firing at intervals. Same angle calculation works for bullet direction. For collision detection between enemy ships, basic hitTestObject works fine unless you need pixel-perfect stuff. Loop through your enemy array and check each ship against others. Pro tip I learned the hard way - don’t put movementTimer.start() inside the event handler or you’ll spawn multiple timers running at once. Move it outside the function.