MouseListener java出错

时间:2016-04-20 12:42:37

标签: java mouselistener

我正在开发一款游戏"当我点击Canvas时遇到问题。 KeyManager无法加载并崩溃。但是当尝试使用KeyEvent加载它时,它可以正常工作。

我的代码是:

    //Click Event   
public class MouseManager implements MouseListener {

    private GameState gameState;
    private Game game;

    public boolean enter;

    public MouseManager(){

    }

    @Override
    public void mouseClicked(MouseEvent e){
        if (State.getState().toString().contains("MenuState")) {
            for (int x = 187; x < 287; x++) {
                for (int y = 400; y < 430; y++) {
                    if (e.getX() == x && e.getY() == y) {
                        this.gameState = new GameState(this.game);
                        State.setState(this.gameState);
                        break;
                    }
                }
            }
        } else {
            return;
        }
    }

    @Override
    public void mouseEntered(MouseEvent e){
    }

    @Override
    public void mouseExited(MouseEvent e){
    }

    @Override
    public void mousePressed(MouseEvent e){
    }

    @Override
    public void mouseReleased(MouseEvent e){
    }
}

  //Game State class

    public GameState(Game game){
        super(game);
        this.player = new Player(game, 100.0F, 100.0F);
        this.friend = new Friend(100.0F, 100.0F);
        this.world = new World("res/worlds/world1.txt");
    }

//State class

private static State currentState = null;
protected Game game;

public static void setState(State state){
    currentState = state;
}

public static State getState(){
    return currentState;
}

public State(Game game){
    this.game = game;
}

public abstract void tick();

public abstract void render(Graphics paramGraphics);

//GameState class

public class GameState extends State {

private Player player;
private Friend friend;
private World world;

public GameState(Game game){
    super(game);
    this.player = new Player(game, 100.0F, 100.0F);
    this.friend = new Friend(100.0F, 100.0F);
    this.world = new World("res/worlds/world1.txt");
}

public void tick(){
    this.world.tick();
    this.player.tick(); //line 25
    this.friend.tick();
}

public void render(Graphics g){
    this.world.render(g);
    this.player.render(g);
    this.friend.render(g);
}

//Player

public class Player extends Creature {

private Game game;
private int onMoveDown = 0;
private int onMoveUp = 0;
private int onMoveLeft = 0;
private int onMoveRight = 0;

public Player(Game game, float x, float y){
    super(x, y, 64, 64);
    this.game = game;
}

private void getInput(){
    this.xMove = 0.0F;
    this.yMove = 0.0F;
    float vol = -40.0F;

    SpriteAngel angel = new SpriteAngel(ImageLoader.loadImage("/sprites/angel.png"));

    if (this.game.getKeyManager().up) { //line 31
        this.yMove = (-this.speed);

        onMoveLeft = 0;
        onMoveRight = 0;
        onMoveDown = 0;

        if (onMoveUp == 6) {
            Assets.player = angel.crop(80, 192, 80, 64);
        }
        if (onMoveUp == 12) {
            Assets.player = angel.crop(160, 192, 80, 64);
        }
        if (onMoveUp == 15) {
            Assets.player = angel.crop(240, 192, 80, 64);
        }
        if (onMoveUp == 21) {
            onMoveUp = 0;
        }
        onMoveUp++;
        Sounds.playSound("res/sounds/walk.wav", vol);
    }

    if (this.game.getKeyManager().down) {
        this.yMove = this.speed;

        onMoveLeft = 0;
        onMoveRight = 0;
        onMoveUp = 0;

        if (onMoveDown == 6) {
            Assets.player = angel.crop(80, 0, 80, 64);
        }
        if (onMoveDown == 12) {
            Assets.player = angel.crop(160, 0, 80, 64);
        }
        if (onMoveDown == 15) {
            Assets.player = angel.crop(240, 0, 80, 64);
        }
        if (onMoveDown == 21) {
            onMoveDown = 0;
        }
        onMoveDown++;
        Sounds.playSound("res/sounds/walk.wav", vol);
    }

    if (this.game.getKeyManager().left) {
        this.xMove = (-this.speed);

        onMoveRight = 0;
        onMoveUp = 0;
        onMoveDown = 0;

        if (onMoveLeft == 6) {
            Assets.player = angel.crop(80, 64, 80, 64);
        }
        if (onMoveLeft == 12) {
            Assets.player = angel.crop(160, 64, 80, 64);
        }
        if (onMoveLeft == 15) {
            Assets.player = angel.crop(240, 64, 80, 64);
        }
        if (onMoveLeft == 21) {
            onMoveLeft = 0;
        }
        onMoveLeft++;
        Sounds.playSound("res/sounds/walk.wav", vol);
    }

    if (this.game.getKeyManager().right) {
        this.xMove = this.speed;

        onMoveLeft = 0;
        onMoveUp = 0;
        onMoveDown = 0;

        if (onMoveRight == 6) {
            Assets.player = angel.crop(80, 128, 80, 64);
        }
        if (onMoveRight == 12) {
            Assets.player = angel.crop(160, 128, 80, 64);
        }
        if (onMoveRight == 15) {
            Assets.player = angel.crop(240, 128, 80, 64);
        }
        if (onMoveRight == 21) {
            onMoveRight = 0;
        }

        onMoveRight++;
        Sounds.playSound("res/sounds/walk.wav", vol);
    }

    if (this.game.getKeyManager().shift) {
        setSpeed(DEFAULT_SPEED + 1.7F);
    } else {
        setSpeed(DEFAULT_SPEED);
    }
}

public void tick(){
    getInput(); //line 132
    move();
}

public void render(Graphics g){
    g.drawImage(Assets.player, (int) this.x, (int) this.y, null);
}

//Game.java:53

    private void tick(){
    this.keyManager.tick();
    this.mouseManager.tick();
    if (State.getState() != null) {
        State.getState().tick();
    }
}

在线出错

if (this.game.getKeyManager().up) {
          }

Loader class

this.display.getCanvas().addMouseListener(this.mouseManager);

错误

Exception in thread "Thread-0" java.lang.NullPointerException
    at com.gmail.cadox8.deud.entities.creatures.Player.getInput(Player.java:31)
    at com.gmail.cadox8.deud.entities.creatures.Player.tick(Player.java:132)
    at com.gmail.cadox8.deud.states.GameState.tick(GameState.java:25)
    at com.gmail.cadox8.deud.Game.tick(Game.java:53)
    at com.gmail.cadox8.deud.Game.run(Game.java:89)
    at java.lang.Thread.run(Unknown Source)

//编辑代码

2 个答案:

答案 0 :(得分:0)

你的mouseClicked()有点奇怪,我想你的意思是

@Override
public void mouseClicked(MouseEvent e){
    if (State.getState().toString().contains("MenuState")) {
        if (e.getX() >= 187
         && e.getX() < 287
         && e.getY() >= 400
         && e.getY() < 430) {
                this.gameState = new GameState(this.game);
                State.setState(this.gameState);
            }
        }
    } else {
        return;
    }
}

但这可能不是你问题的原因,请查看Game.java:89 什么是null。

答案 1 :(得分:0)

注册mouseListener时,请尝试如下:.addMouseListener(this); 我自己有点新鲜,但似乎我过去就这样做了。