Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8913235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:24:30+00:00 2026-06-15T04:24:30+00:00

In my game I’m making, I need NPC’s/mobs. I made a class called peasant

  • 0

In my game I’m making, I need NPC’s/mobs.
I made a class called peasant (my first mob).
In the main class from which the game is running, I have all the information, as well as calling an object called mob1 from Peasant
I need to make it so that if the player is within 300 pixels of the mob, it starts to move towards it. Ive tried doing this but so far, even if the player is 2000 pixels away, the mob starts moving???

Here is my Peasant class

package Entitys.NPCs;


public class Peasant {

    public Peasant(float InitX, float InitY, int MobID){
        MobX = InitX;
        MobY = InitY;
    }
    //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;
    public boolean PlayerDetected = false;
    long timer;
    //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) {
        // If the player has not been spotted the NPC/Mob will move left and
        // right by 100 Pixels.
        if(MobX-300<playerX){
            PlayerDetected=true;
        }

        if(PlayerDetected=true){
            if(MobX>playerX){
                MobX-=delta*0.12;
            }
        }

    }

}

And here is the main class:

    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 Entitys.NPCs.*;

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;
    Peasant mob1;
    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;
        WorldX = 0;
        WorldY = 0;
        world= new Image("res/WorldImages/WorldBackGround.png");
        mousePosition="null";
        MovementDirection = "Not Moved Yet";
        mob1= new Peasant(2000, 200, 1);
        if(WorldX<=0){
            playerX = Math.abs(WorldX);
        }else{
            playerX=0-WorldX;
        }
    }

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

        //All this is co-ords ect, it is for developement help
        g.drawString(String.valueOf(mob1.getX()), 50, 200);
        g.drawString("World X: "+ String.valueOf(WorldX), 50, 225);
        g.drawString("Player X: "+ String.valueOf(playerX), 50, 250);
        g.drawString("Player Detetcted?: "+ String.valueOf(mob1.PlayerDetected), 50, 265);
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
        if(WorldX<=0){
            playerX = Math.abs(WorldX);
        }else{
            playerX=0-WorldX;
        }
        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;
    }

}

The autoEveryThing method in the Peasant class should make it so that if(mobX-300

But when I first run this it starts moving towards the player?
even though (2000-300<0) is false, it still sets the PlayerDetected boolean to true???
Why does this happen?
Thanks

EDIT:
After trying to go through thi and fix it I found somthing strange, even if I take out the whole bit which can change PlayerDetected to true, the mob still moves towards the player. This meens that PlayerDetected is becominbg true somwhere, but I cant figure out where?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:24:32+00:00Added an answer on June 15, 2026 at 4:24 am
       if(PlayerDetected=true){
    

    is wrong you should use ==

       if(PlayerDetected==true){
    

    or even better

    boolean isPlayerDetected;
    
    if (isPlayerDetected) 
    

    further consider

     double dx = mobX - playerX;
     double dy = mobY - playerY;
     double distance = Math.sqrt(dx*dx + dy*dy);
     if (distance < 300) {
         // is within distacne threshold
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Multuplayer game with socket.io - Make the bigger word from letters How need to
Jump Game: Given an array, start from the first element and reach the last
Making game of life I need to a have a grid that is 30x20
I am making a game in java and I want to create a simulation
I have a game which displays an array of colored blocks. The user can
I am porting a game using SpiderMonkey to Android. Because I need to integrate
My game application is having a timer(starting from 0:00 then increases) when it is
I'm making a game that will allow content development and I'd like it to
Making an uno game for a project. all 108 cards are located in a
My game (Mac OS X 10.5 compatible) needs a feature to switch(minimize) from fullscreen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.