Trouble with AI in AS3

75 Views Asked by At

I'm making a battleship simulator, in which you navigate a battleship to complete a certain objective whilst fending off enemy ships. Said enemy ships should have a form of primitive AI that would allow it to track the location of your ship and attempt to shoot at it.

The issue I am having is that I'm not entirely sure how I can code for a group of enemy ships to hone in on the player's ship based on location. I have made multiple attempts to get the enemy ship to move, but to no avail.

Attached is my most recent coding attempt for enemy ship movement (mvi_PBattleship is for the player ship and mvi_EBattleship is for the enemy ship):

    var EMoveTimer:Timer = new Timer(10);
    EMoveTimer.addEventListener("timer", EMove);

    function EMove(eventArgs:TimerEvent):void{

        EMoveTimer.start();

        if(((mvi_EBattleship.x + 31.7) < mvi_PBattleship.x) && (mvi_EBattleship.x < mvi_PBattleship.x) && ((mvi_EBattleship.y + 195.8) < mvi_PBattleship.y) && (mvi_EBattleship.y < mvi_PBattleship.y)){
            mvi_EBattleship.x += 1;
            mvi_EBattleship.y += 1;
        }

        else if(((mvi_EBattleship.x + 31.7) < mvi_PBattleship.x) && (mvi_EBattleship.x < mvi_PBattleship.x) && (mvi_EBattleship.y > (mvi_PBattleship.y + 195.8)) && ((mvi_EBattleship + 195.8) > (mvi_PBattleship.y + 195.8))){
            mvi_EBattleship.x += 1;
            mvi_EBattleship.y -= 1;
        }

        else if((mvi_EBattleship.x > (mvi_PBattleship.x + 31.7)) && ((mvi_EBattleship.x + 31.7) > (mvi_PBattleship.x + 31.7)) && ((mvi_EBattleship.y + 195.8) < mvi_PBattleship.y) && (mvi_EBattleship.y < mvi_PBattleship.y)){
            mvi_EBattleship.x -= 1;
            mvi_EBattleship.y += 1;
        }

        else if((mvi_EBattleship.x > (mvi_PBattleship.x + 31.7)) && ((mvi_EBattleship.x + 31.7) > (mvi_PBattleship.x + 31.7)) && (mvi_EBattleship.y > (mvi_PBattleship.y + 195.8)) && ((mvi_EBattleship + 195.8) > (mvi_PBattleship.y + 195.8))){
            mvi_EBattleship.x -= 1;
            mvi_EBattleship.y -= 1;
        }

        else if((mvi_EBattleship.x > mvi_PBattleship.x) && ((mvi_EBattleship.x + 31.7) < (mvi_PBattleship.x + 31.7)) && ((mvi_EBattleship.y + 195.8) < mvi_PBattleship.y) && (mvi_EBattleship.y < mvi_PBattleship.y)){
            mvi_EBattleship.y += 1;
        }

        else if((mvi_EBattleship.x > mvi_PBattleship.x) && ((mvi_EBattleship.x + 31.7) < (mvi_PBattleship.x + 31.7)) && (mvi_EBattleship.y > (mvi_PBattleship.y + 195.8)) && ((mvi_EBattleship.y + 195.8) > (mvi_PBattleship.y + 195.8))){
            mvi_EBattleship.y -= 1;
        }

        else if((mvi_EBattleship.y > mvi_PBattleship.y) && ((mvi_EBattleship.y + 195.8) < (mvi_PBattleship.y + 195.8)) && ((mvi_EBattleship.x + 31.7) < mvi_PBattleship.x) && (mvi_EBattleship.x < mvi_PBattleship.x)){
            mvi_EBattleship.x += 1;
        }

        else if((mvi_EBattleship.y > mvi_PBattleship.y) && ((mvi_EBattleship.y + 195.8) < (mvi_PBattleship.y + 195.8)) && (mvi_EBattleship.x > (mvi_PBattleship.x + 31.7)) && ((mvi_EBattleship.x + 31.7) > (mvi_PBattleship.x + 31.7))){
            mvi_EBattleship.x += 1;
        }
    }

So, my main questions are:

1) What code should I be using to ensure that the enemy will track down the player?

2) How would I make the enemy fire bullets at the player?

3) Is there a way to track collisions between two enemy ships?

Actual code would be much needed and appreciated, owing to the fact this project is expected to be near completion by tomorrow. Thank you very much for your help.

1

There are 1 best solutions below

0
SharpEdge On

I'm going to answer only the first question that is the must important from what i see.

To control an NPC ( Non-Player Character ) you can use what is called "steering behaviour" here a very good guide for as3.

Don't reinvent the wheel!