【发布时间】:2020-06-12 01:31:00
【问题描述】:
我正在用 java 制作游戏,所以我需要一些帮助。 我想在包中组织我的代码,但我不明白属于哪个 .java 文件。 My file organization
App.java
package main;
import engine.io.Window;
import org.lwjgl.glfw.GLFW;
public class App implements Runnable {
public Thread game;
public static Window window;
public static final int WIDTH = 1280, HEIGHT = 760;
public void start() {
game = new Thread(this, "game");
game.start();
}
public static void init() {
window = new Window(WIDTH, HEIGHT, "Game");
window.create();
}
public void run() {
init();
while (!window.shouldClose()) {
update();
render();
}
}
private void update() {
window.update();
}
private void render() {
window.swapBuffers();
}
public static void main(String[] args) {
new App().start();
}
}
还有Window.java
package engine.io;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWVidMode;
public class Window {
private int width, height;
private String title;
private long window;
public int frames;
public static long time;
public Window(int width, int height, String title) {
this.width = width;
this.height = height;
this.title = title;
}
public void create() {
if (!GLFW.glfwInit()) {
System.err.println("ERROR: GLFW wasn't initializied");
return;
}
window = GLFW.glfwCreateWindow(width, height, title, 0, 0);
if (window == 0) {
System.err.println("ERROR: Window wasn't created");
return;
}
GLFWVidMode videoMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
GLFW.glfwSetWindowPos(window, (videoMode.width() - width) / 2, (videoMode.height() - height) / 2);
GLFW.glfwMakeContextCurrent(window);
GLFW.glfwShowWindow(window);
GLFW.glfwSwapInterval(1);
time = System.currentTimeMillis();
}
public void update() {
GLFW.glfwPollEvents();
frames++;
if (System.currentTimeMillis() > time + 1000) {
GLFW.glfwSetWindowTitle(window, title + " | FPS: " + frames);
time = System.currentTimeMillis();
frames = 0;
}
}
public void swapBuffers() {
GLFW.glfwSwapBuffers(window);
}
public boolean shouldClose() {
return GLFW.glfwWindowShouldClose(window);
}
}
我收到以下错误: 声明的包“main”与预期的包“ourRTS”不匹配 和 无法解析导入引擎。
【问题讨论】:
-
我不会将包命名为“main”。我会使用 engine.io 作为包。你有域名吗?许多人使用 ip-name 来保留自己的名字,例如 'package engine.foobar.com;'
-
你的目录结构与包名匹配吗? IE。
App.java在名为main/和Window.java在engine/io/的文件夹中,与您项目的根目录相关吗?你使用的是什么目录布局?