Java,Slick,一个不应该移动的对象正在移动?

时间:2012-11-30 17:14:17

标签: java oop slick2d

在我正在制作的游戏中,我需要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布尔值设置为true ??? 为什么会这样? 感谢

编辑: 在尝试通过这个并修复它后,我发现了一些奇怪的东西,即使我取出可以改变PlayerDetected为真的整个位,暴徒仍然朝着玩家移动。玩家检测到的这个mecominbg是真实的somwhere,但我无法弄清楚在哪里?

1 个答案:

答案 0 :(得分: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
 }