简体   繁体   English

Java Applet 游戏循环停止鼠标/键盘输入?

[英]Java Applet Game Loop stops Mouse/Keyboard Input?

I got a problem I couldn't get to work after about 2 Hours of trying.经过大约 2 小时的尝试后,我遇到了一个无法上班的问题。 I want to have a loop that do 2 Methods (Draw and update) but also listen to Mouse/Keyboard events.我想要一个循环来执行 2 种方法(绘制和更新),但还要听鼠标/键盘事件。 I have a loop that Draws and Updates, but does nothing outside of the loop ( Listening to events ) I tried alot of things but nothing worked.我有一个绘制和更新的循环,但在循环之外什么都不做(监听事件)我尝试了很多东西,但没有任何效果。 Help Please?请帮忙?

I tried using the Runnable Thread, using different orders, using wait() and notify(), I've tried alot of things.我尝试使用 Runnable Thread,使用不同的命令,使用 wait() 和 notify(),我尝试了很多东西。 But basicly I want to know how to run a loop and still check for User Input但基本上我想知道如何运行循环并仍然检查用户输入

Also when I try to quit the program clicking the red "X", it won't quit but still work此外,当我尝试单击红色“X”退出程序时,它不会退出但仍然可以工作

Here's the Code:这是代码:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class main extends Applet implements MouseListener, Runnable {

    public main() {
        super();
        init();
    }
    Thread t;
    Screen screen = new Screen();
    String Text = "Hello";
    boolean Running = true;
    boolean Click = false;
    int R = 0x00;
    int G = 0x00;
    int B = 0x00;
    int xpoints[] = {25, 40, 40, 25, 25};
    int ypoints[] = {40, 40, 25, 25, 25};
    int npoints = 5;

    public void run() {
        while (Running) {
            GameLoop();
        }
    }

    public void init() {
        this.addMouseListener(this);
        this.setSize(400, 300); //manually set your Frame's size
        t = new Thread(this);
        t.start();
    }

    public void paint(Graphics g) {
        g.setColor(new Color(R, B, G));
        g.fillPolygon(xpoints, ypoints, npoints);
        Running = true;
        t.run();
    }

    public void mousePressed(MouseEvent e) { //On Mouse Click
        System.exit(0);
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        System.exit(0);
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public boolean keyDown(Event e, int key) {
        return true;
    }

    public void GameLoop() {
        if (Running) {
            if (R != 0xff) {
                R++;
            } else {
                if (G != 0xff) {
                    G++;
                } else {
                    if (B != 0xff) {
                        B++;

                    } else {
                        System.exit(0);
                    }
                }
            }
            try {
                sleep(20);
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
            paint(getGraphics());
        }
    }

    public void sleep(int time) throws InterruptedException {
        Thread.sleep(time, 0);
    }
}

This tutorial should provide some insight as to how your program should be structured.本教程应该提供一些关于如何构建程序的见解。 And this one is helpful for the mouse listener.对鼠标听众很有帮助。

Issues you should address:您应该解决的问题:
1) You're doing something fishy with the paint method. 1)你正在用paint方法做一些可疑的事情。 Why are you calling t.run() in there?你为什么在那里调用t.run() The thread t is already running and looping constantly calling the paint() method to redraw the screen.线程t已经在运行并且循环不断地调用paint()方法来重绘屏幕。 Remove this call and see what you get.删除这个电话,看看你得到了什么。

1) The destruction of your thread/applciation is poor. 1)您的线程/应用程序的破坏很差。 The first example above provides a better way for that to occur上面的第一个例子提供了一种更好的方式来实现这一点

2) You have your System.Exit(0) on mousePressed() with the comment //on mouse click but nothing in mouseClicked() ... it works but its bad convention 2)您在mousePressed()上有System.Exit(0)并带有注释//on mouse click但在mouseClicked()中没有任何内容 ...它可以工作,但它的约定不好

3)Having your class named main is extremely poor convention that is both confusing and impractical. 3)将 class 命名为main是非常糟糕的约定,既令人困惑又不切实际。 Rename your class to something like "Game" or similar.将您的 class 重命名为“游戏”或类似名称。

4) Why declare Screen if you don't use it? 4)如果不使用Screen ,为什么要声明它?

I see that you define a Running variable to be true upon initialization.我看到您在初始化时将 Running 变量定义为 true。 This variable is used to determine whether or not the game should stop.此变量用于确定游戏是否应该停止。 I, however, don't see any place where you modify the value of this variable to false.但是,我没有看到您将此变量的值修改为 false 的任何地方。 This would explain why your game never exits.这可以解释为什么你的游戏永远不会退出。

As for the the game not working, try debugging the application in an IDE.至于游戏无法运行,请尝试在 IDE 中调试应用程序。 You should then pay attention to what, if any, Exception are being thrown and the values of any variables you are questioning.然后,您应该注意抛出了什么异常(如果有的话)以及您所质疑的任何变量的值。 Hopefully this will give you insight into the behavior of your app.希望这能让您深入了解应用程序的行为。

Don't forget to update us with any new info you discover so we can help you out along the way.不要忘记向我们提供您发现的任何新信息,以便我们一路为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM