简体   繁体   English

使用LWJGL的Keyboard类时出现java.lang.StackOverflowError

[英]java.lang.StackOverflowError while using LWJGL's Keyboard class

I have been trying to implement a Keyboard class into my game and received the exception below. 我一直在尝试在我的游戏中实现Keyboard类,并收到以下异常。 This is just a snippet. 这只是一个片段。 The full exception goes on for ages longer than this. 完全例外的年龄比这更长。

Exception in thread "main" java.lang.StackOverflowError
at org.lwjgl.opengl.DisplayMode.<init>(DisplayMode.java:63)
at oregon.client.Oregon.<init>(Oregon.java:10)
at oregon.src.Controller.<init>(Controller.java:9)
at oregon.client.Oregon.<init>(Oregon.java:12)
at oregon.src.Controller.<init>(Controller.java:9)
at oregon.client.Oregon.<init>(Oregon.java:12)
at oregon.src.Controller.<init>(Controller.java:9)

Here's the code for the main class (oregon.client.Oregon): 这是主类(oregon.client.Oregon)的代码:

package oregon.client;

import oregon.src.Controller;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class Oregon {
    public DisplayMode normal = new DisplayMode(640, 640);

    public Controller controls = new Controller();

    public boolean fullscreen = false;

    public void start() {
        try {
            create();
        } catch (LWJGLException e) {
            stop(e);
        }

        while (!Display.isCloseRequested()) {
            events();
            Display.update();
        }

        Display.destroy();
    }

    public void events() {
        try {
            controls.getInput();
        } catch (LWJGLException e) {
            stop(e);
        }
    }

    public void setFullscreen() {
        try {
            if (!fullscreen) {
                Display.setFullscreen(true);
                fullscreen = true;
            } else if (fullscreen) {
                Display.setDisplayMode(normal);
                fullscreen = false;
            }
        } catch (LWJGLException e) {
            stop(e);
        }
    }

    public void create() throws LWJGLException {
        if (fullscreen) {
            Display.setFullscreen(true);
        } else if (!fullscreen) {
            Display.setDisplayMode(normal);
        }

        Display.create();
    }

    public void stop() {
        System.exit(0);
        Display.destroy();
    }

    public void stop(Exception e) {
        e.printStackTrace();
        System.exit(0);
        Display.destroy();
    }

    public static void main(String args[]) {
        Oregon oregon = new Oregon();
        oregon.start();
    }
}

And here's the code for my keyboard class:- 这是我的键盘类的代码:-

package oregon.src;

import oregon.client.Oregon;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;

public class Controller {
    public Oregon oregon = new Oregon();

    public void getInput() throws LWJGLException {
        while (Keyboard.next()) {
            if (Keyboard.getEventKeyState()) {
                if (Keyboard.getEventKey() == Keyboard.KEY_F11) {
                    oregon.setFullscreen();
                }
            }
        }
    }
}

If there is an expert out the with LWJGL, could you please help me out? 如果LWJGL有专家陪同,请您帮帮我吗? Thank you and I hope I do get some help. 谢谢,希望我能有所帮助。 :D :d

Nothing to do with LWJGL. 与LWJGL无关。 Stack overflows in simple code are always because of accidental infinite loops. 简单代码中的堆栈溢出总是由于偶然的无限循环造成的。 You have one: Controller tries to create an Oregon (this line: public Oregon oregon = new Oregon();) , which then tries to create a Controller , which tries to... (etc..) 您有一个: Controller尝试创建一个Oregon (此行: public Oregon oregon = new Oregon();) ,然后尝试创建一个Controller ,该Controller尝试...(等等。)

When you create an Oregon instance, it creates a Controller instance, which creates an Oregon instance, which creates a Controller instance, which creates an... 创建Oregon实例时,它会创建一个Controller实例,该实例将创建一个Oregon实例,该实例将创建一个Controller实例,然后创建一个...

What you should probably be doing is not creating an Oregon instance in your controller, but passing your existing instance as a parameter to the Controller constructor and storing that (or the other way around). 您可能应该做的不是在控制器中创建Oregon实例,而是将现有实例作为参数传递给Controller构造函数并将其存储(或相反)。

Pseudo code: 伪代码:

public Oregon() {
  controller = new Controller(this);
  ...
}
public Controller(Oregon oregon) {
  this.oregon = oregon;
  ...
}

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

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