0

在我制作的游戏中,我需要 NPC/小怪。我创建了一个名为农民(我的第一个暴民)的课程。在游戏运行的主类中,我拥有所有信息,以及从 Peasant 调用一个名为 mob1 的对象,我需要制作它,以便如果玩家在暴徒的 300 像素内,它开始向它。我试过这样做,但到目前为止,即使玩家在 2000 像素之外,暴徒也会开始移动???

这是我的农民课

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;
            }
        }

    }

}

这是主要课程:

    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;
    }

}

Peasant 类中的 autoEveryThing 方法应该使得 if(mobX-300

但是当我第一次运行它时它开始向玩家移动?即使 (2000-300<0) 为假,它仍将 PlayerDetected 布尔值设置为真???为什么会这样?谢谢

编辑:在尝试通过 thi 并修复它之后,我发现了一些奇怪的东西,即使我取出可以将 PlayerDetected 更改为 true 的整个位,暴徒仍会向玩家移动。这意味着 PlayerDetected 在某个地方变得真实,但我不知道在哪里?

4

1 回答 1

1
   if(PlayerDetected=true){

错了,你应该使用 ==

   if(PlayerDetected==true){

甚至更好

boolean isPlayerDetected;

if (isPlayerDetected) 

进一步考虑

 double dx = mobX - playerX;
 double dy = mobY - playerY;
 double distance = Math.sqrt(dx*dx + dy*dy);
 if (distance < 300) {
     // is within distacne threshold
 }
于 2012-11-30T17:47:23.347 回答