0

i need help creating enemy. i want enemy to move closer to player and enemy can only move left or right;

here is my enemy class

public class Enemy extends BasicGameState
{
    Animation enemy;
    Animation e_movingRIGHT;
    Animation e_movingLEFT;

    int[] duration = {200, 200}; //how fast animation change
    double speed = 0.3;

    /*** constructor Method***/
    public Enemy(int state)
    {
    }

    /* create variables with new */
    public void init(GameContainer gc, StateBasedGame sbg)  throws SlickException
    {
        Image[] e_walkRIGHT = {new Image("res/personRight.png"), new Image("res/personRight.png")};
        Image[] e_walkLEFT = {new Image("res/personLeft.png"), new Image("res/personLeft.png")};

        e_movingRIGHT = new Animation(e_walkRIGHT, duration, false);
        e_movingLEFT = new Animation(e_walkLEFT, duration, false);
    }

    /* draw stuff on screen */
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g)  throws SlickException
    {
    }

    /* Animations */
    public void update(GameContainer gc, StateBasedGame sbg, int delta)  throws SlickException
    {
    }

    //return state - menu = 0, play = 1
        public int getID()
        {
            return 1;
        }


}




here is my player class
public class Play extends BasicGameState
{
    /*** Variables ***/
    Image worldMAP;
    Animation person;
    Animation movingUP;
    Animation movingDOWN;
    Animation movingRIGHT;
    Animation movingLEFT;

    boolean quit = false;
    int[] duration = {200, 200}; //how fast animation change

    double speed = 0.3;
    float personX = 0;  //keep track of x
    float personY = 0; //keep track of y
    float shiftX = personX + 320;  //starting position
    float shiftY = personY + 160;   //starting position


    /*** constructor Method***/
    public Play(int state)
    {

    }

    /* create variables with new */
    public void init(GameContainer gc, StateBasedGame sbg)  throws SlickException
    {
        worldMAP = new Image("res/world.png");
        Image[] walkUP = {new Image("res/personBack.png"), new Image("res/personBack.png")};
        Image[] walkDOWN = {new Image("res/personFront.png"), new Image("res/personFront.png")};
        Image[] walkRIGHT = {new Image("res/personRight.png"), new Image("res/personRight.png")};
        Image[] walkLEFT = {new Image("res/personLeft.png"), new Image("res/personLeft.png")};

        movingUP = new Animation(walkUP, duration, false);
        movingDOWN = new Animation(walkDOWN, duration, false);
        movingRIGHT = new Animation(walkRIGHT, duration, false);
        movingLEFT = new Animation(walkLEFT, duration, false);

        person = movingDOWN;
    }

    /* draw stuff on screen */
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g)  throws SlickException
    {
        worldMAP.draw(personX, personY);
        person.draw(shiftX, shiftY);
        g.drawString("Person X: "+personX, 400, 20);
        g.drawString("Person Y: "+personY, 400, 40);

        if(quit == true)
        {
            g.drawString("Resume(R)",250,100);
            g.drawString("Main Menu(M)",250,150);
            g.drawString("Quit Game(Q)",250,200);
            if(quit == false)
            {
                g.clear();  //clear all draws
            }
        }
    }

    /* Animations */
    public void update(GameContainer gc, StateBasedGame sbg, int delta)  throws SlickException
    {
        Input input = gc.getInput();
        if(input.isKeyDown(Input.KEY_UP))
        {
            person = movingUP;
            personY += delta * speed; //move 1 px up
            if(personY > 162)      //collision - cant move above y162
            {
                personY -= delta * speed;
            }
        }
        //down key
        else if(input.isKeyDown(Input.KEY_DOWN))
        {
            person = movingDOWN;
            personY -= delta * speed;
            if(personY < -600)      //collision - cant move below Y600
            {
                personY += delta * speed;
            }
        }
        //left key
        else if(input.isKeyDown(Input.KEY_LEFT))
        {
            person = movingLEFT;
            personX += delta * speed;
            if(personX > 324)      //collision - cant move left x324
            {
                personX -= delta * speed;
            }
        }
        //right key
        else if(input.isKeyDown(Input.KEY_RIGHT))
        {
            person = movingRIGHT;
            personX -= delta * speed;
            if(personX < -840)      //collision - cant move above y162
            {
                personX += delta * speed;
            }
        }

        //escape key
        if(input.isKeyDown(Input.KEY_ESCAPE))
        {
            quit = true;
        }
        //when menu is up
        if(quit == true)
        {
            if(input.isKeyDown(Input.KEY_R))
            {
                quit = false;
            }   
            if(input.isKeyDown(Input.KEY_M))
            {
                sbg.enterState(0);
            }
            if(input.isKeyDown(Input.KEY_Q))
            {
                System.exit(0);
            }
        }
    }

    //return state - menu = 0, play = 1
    public int getID()
    {
        return 1;
    }
}
2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by NormR1
0

i want enemy to move closer to player

Test if the player is to left (minus) or right (plus) and change the x value accordingly.
The player is to the left if its x value is less than the enemy's x value.

Edited by NormR1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.