Java - MouseListener不起作用

时间:2017-11-19 18:35:47

标签: java awt mouseevent event-listener mouselistener

我一直在尝试为基本的Java游戏创建一个功能按钮,但每当我点击屏幕时(字面上我在参数内外的任何地方),我的游戏中都没有功能。我试图弄清楚我的冲突在哪里鼠标事件不起作用。我有几乎所有的双重和三重检查,我找不到问题。我包含了Game Class和Menu类,但如果需要其他类,我会发布它们。我也知道代码很可能非常草率和不合适,但我只是在学习并尝试自学一些基础知识。

问题是" STATE"我创建的枚举?

问题可以在boolean mouseOver中的某个地方吗?

我非常困惑,不明白我到底搞砸了。我希望能够拥有游戏所需的功能。很抱歉,如果这是一个非常基本的问题,或者代码很糟糕,我会尽力提供帮助。

package com.test.main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;

public class Game extends Canvas implements Runnable {
    private static final long serialVersionUID = 5348695609807625849L;
    public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;
    private Thread thread;
    private boolean running = false;
    private Random r;
    private Handler handler;
    private HUD hud;
    private Spawn spawner;
    private Menu menu;

    public enum STATE {
        Menu,
        Game
    };

    public STATE gameState = STATE.Menu;

    public Game(){
        handler = new Handler();
        menu = new Menu(this, handler);
        this.addKeyListener(new KeyInput(handler));
        this.addMouseListener(menu);


        new Window(WIDTH, HEIGHT, "GafGin", this);

        hud = new HUD();
        spawner = new Spawn(handler, hud);
        r = new Random();

        if(gameState == STATE.Game) {
            handler.addObject(new Player(WIDTH/2-32, HEIGHT/2-32, ID.Player, handler));
            handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 50), r.nextInt(Game.HEIGHT - 50), ID.BasicEnemy, handler));
        }
    }

    public synchronized void start() {
        thread = new Thread(this);
        thread.start();
        running = true;
    }

    public synchronized void stop() {
        try {
            thread.join();
            running = false;
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void run() {
        this.requestFocus();
        long lastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        while(running) {
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            while(delta >= 1) {
                tick();
                delta--;
                gameOver();
            }
            if(running)
                render();
            frames++;

            if(System.currentTimeMillis() - timer > 1000) {
                timer+= 1000;
                System.out.println("FPS: " + frames);
                frames = 0;
            }
        }
        stop();
    }

    private void tick() {
        handler.tick();
        if(gameState == STATE.Game) {
            hud.tick();
            spawner.tick();
        }else if(gameState == STATE.Menu) {
            menu.tick();
        }
    }

    private void render() {
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null) {
            this.createBufferStrategy(3);
            return;
        }

        Graphics g = bs.getDrawGraphics();

        g.setColor(Color.black);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        handler.render(g);

        if(gameState == STATE.Game) {
            hud.render(g);
        }else if(gameState == STATE.Menu) {
            menu.render(g);
        }

        g.dispose();
        bs.show();
    }

    public static float clamp(float var, float min, float max) {
        if(var >= max)
            return (var = max);
        else if(var <= min)
            return (var = min);
        else
            return var;
    }

    public void gameOver() {
        if(HUD.HEALTH <= 0) {
            handler.endGame();
        }
        checkRunning();
    }

    public void checkRunning() {
        if(Handler.getGameEnded() == true) {
            running = false;
        }
    }

    public static void main(String[] args) {
        new Game();
    }

}

package com.test.main;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import com.test.main.Game.STATE;

public class Menu extends MouseAdapter{
    private Game game;
    private Handler handler;
    private Random r = new Random();

    public Menu(Game game, Handler handler) {
        this.game = game;
        this.handler = handler;
    }

    public void MousePressed(MouseEvent e) {
        int mx = e.getX();
        int my = e.getY();

        if(mouseOver(mx, my, (Game.WIDTH / 2) - 140, (Game.HEIGHT / 2) - 64, 250, 64)){
            game.gameState = STATE.Game;
            handler.addObject(new Player(Game.WIDTH/2-32, Game.HEIGHT/2-32, ID.Player, handler));
            handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 50), r.nextInt(Game.HEIGHT - 50), ID.BasicEnemy, handler));
        }

        if(mouseOver(mx, my, 100, 100, 250, 64)) {
            game.gameState = STATE.Game;
            handler.addObject(new Player(Game.WIDTH/2-32, Game.HEIGHT/2-32, ID.Player, handler));
            handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 50), r.nextInt(Game.HEIGHT - 50), ID.BasicEnemy, handler));
        }
    }

    public void MouseReleased(MouseEvent e) {

    }

    private boolean mouseOver(int mx, int my, int x, int y, int width, int height) {
        if(mx > x && mx < x + width) {
            if(my > y && my < y + height) {
                return true;
            }else return false;
        }else return false;
    }

    public void tick() {

    }

    public void render(Graphics g) {
        Font fnt = new Font("arial", 1, 50);
        Font fnt2 = new Font("arial", 1, 30);

        g.setColor(Color.white);
        g.drawRect(100, 100, 250, 64);

        g.setFont(fnt);
        g.setColor(Color.white);
        g.drawString("Menu", (Game.WIDTH / 2) - 85, (Game.HEIGHT / 2) - 124);

        g.setFont(fnt2);
        g.setColor(Color.white);
        g.drawRect((Game.WIDTH / 2) - 140, (Game.HEIGHT / 2) - 64, 250, 64);
        g.drawString("Play", (Game.WIDTH / 2) - 52, (Game.HEIGHT / 2) - 22);

        g.setColor(Color.white);
        g.drawRect((Game.WIDTH / 2) - 140, (Game.HEIGHT / 2), 250, 64);
        g.drawString("Quit", (Game.WIDTH / 2) - 52, (Game.HEIGHT / 2) + 42);

        g.setColor(Color.white);
        g.drawRect((Game.WIDTH / 2) - 140, (Game.HEIGHT / 2) + 64, 250, 64);
        g.drawString("Help", (Game.WIDTH / 2) - 52, (Game.HEIGHT / 2) + 106);
    }
}

0 个答案:

没有答案