-1

'Im making a simple game, and for part of it I want there to be enemy's which will attack you. To do this first I need to make them move In this code the "Enemy" Is just a ball. I'm using an object called "mob1" as the balls location, so that later on I will be able to have multiple ones.

(Btw, Im using Slick- just incase that changes anything)

My Game State-

    package Worlds.World1;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import Main.SimpleMob;

public class World1A extends BasicGameState{
    String mousePosition;
    Image world;
    Animation player, playerLeft, playerRight;
    int[] duration = {200,200};
    float playerX;
    float playerY;
    float WorldX;
    float WorldY;
    float PlayerVisibleScreenX;
    float PlayerVisibleScreenY;
    String MovementDirection;
    SimpleMob mob1 = new SimpleMob();
    public World1A(int state){
    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
        Image [] WalkingLeft = {new Image("res/Sprites/buckysLeft.png"),new Image("res/Sprites/buckysLeft.png")};
        Image [] WalkingRight = {new Image("res/Sprites/buckysRight.png"),new Image("res/Sprites/buckysRight.png")};

        playerLeft = new Animation(WalkingLeft, duration, false);
        playerRight = new Animation(WalkingRight, duration, false);
        player = playerRight;
        playerX = 0;
        playerY = 0;
        WorldX = 0;
        WorldY = 0;
        world= new Image("res/WorldImages/WorldBackGround.png");
        mousePosition="null";
        MovementDirection = "Not Moved Yet";
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
        world.draw(WorldX, WorldY);
        g.setColor(Color.white);
        g.fillOval(WorldX+mob1.getX(), WorldY+mob1.getY(), 50, 50);
        g.fillRect(WorldX, WorldY+300, 500, 10);
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
        mob1.autoEveryThing(delta, playerX, playerY);
        int posX = Mouse.getX();
        int posY = Mouse.getY();
        mousePosition = "X: " + posX + "\nY: " + posY;

        Input input = gc.getInput();
        if(input.isKeyDown(Input.KEY_LEFT)){
            WorldX += delta * 0.1f;
            MovementDirection = "Left";
            player = playerLeft;
        }else if(input.isKeyDown(Input.KEY_RIGHT)){
            WorldX -= delta * 0.1f;
            MovementDirection = "Right";
            player = playerRight;
        }else{
            MovementDirection = "Not Moving";
        }
    }


    //DO NOT CHANGE
    public int getID(){
        return 2;
    }

}

And my mob class-

 package Main;

import org.newdawn.slick.Graphics;

public class SimpleMob {

    //This class shall be used as an object creator. This will randomly move a graphic around, near to a player
    private float MobX;
    private float MobY;
    private int AmountMoved = 0;
    private boolean MoveRight = true;
    private boolean MoveLeft;
    private boolean PlayerNear = false;
    public boolean PlayerDetected = false;

    //Used to find the mobs X
    public float getX(){
        return MobX;
    }

    //Used to find the mobs Y
    public float getY(){
        return MobY;
    }

    //Used to set the mobs X
    public void setX(float X){
        MobX = X;
    }   

    //Used to set the mobs Y
    public void setY(float Y){
        MobY = Y;
    }

    //Used to simply move the mob on its X co-ords
    public void moveX(int delta){
        MobX += delta*0.1f;
    }

    //Used to simply move the mob on its Y co-ords
    public void moveY(int delta){
        MobY += delta*0.1f;
    }

    public void autoEveryThing(int delta, float playerX, float playerY) {

        System.out.println(AmountMoved);
        // If the player has not been spotted the NPC/Mob will move left and
        // right by 100 Pixels.
        if (MoveRight == true) {
            AmountMoved++;
            MobX += delta * 0.1f;
            if (AmountMoved == 100) {
                MoveRight = false;
                MoveLeft = true;
                AmountMoved = 0;
            }
        }
        if (MoveLeft == true) {
            MobX -= delta * 0.1f;
            AmountMoved++;
            if (AmountMoved == 100) {
                MoveRight = true;
                MoveLeft = false;
                AmountMoved = 0;
            }
        }
    }

}

Any help on this would be good, (Or if theres a more correct/easyier way to make mobs)

Thanks

1 Answers1

0

The problem may be your System.out.println(...) call in autoEveryThing(...). This is being done many times a second, and outputting to the console is one of the most intensive things one can do.

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
  • No, it isnt this. The System.out.println() I added afterwards, to see if it was somthing to do with the AmountMoved integer, removing it makes no differance – Nikita Pond Nov 29 '12 at 18:29
  • Well, removing the system.out.println did remove some of the jagged movement, but I still get quite alot of it. – Nikita Pond Nov 29 '12 at 18:30
  • I'm still confused by what you mean by jaggedy :) You mean not a smooth movement with relation to time, or moving in a different way than it's supposed to? – Chris Dennett Nov 29 '12 at 18:37
  • Well Originaly the ball would move about 50 pixels right, move back about 10, move forwards about 10, move back about 10 (and do this for a few seconds) then it would carry on till it reached a total X of 100. – Nikita Pond Nov 29 '12 at 18:43
  • But then it would do the same on the way back,. The extra problem of this is that the X co-ords wont go up and down equaly. So It will start at the very top lef tof the screen, go about 80 or 120 (cause of the 'Jaggedy' movement) then come back by 100, and so go off the screen – Nikita Pond Nov 29 '12 at 18:44