我正在尝试制作自己的引擎,但我需要一些帮助。
我目前正在进行关卡系统。 level类扩展了render类,level类覆盖了Render类的render方法。最后,从主类调用render类,但我不调用level类。
修改
我已删除静态但现在无法调用render方法。我知道我很自我,有点教我自己。
package SimpleEngine.Render;
渲染类(这称为)
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import SimpleEngine.Primitives.*;
import static org.lwjgl.opengl.GL11.glClear;
public class Render {
public void Render() {
}
}
Level Class(未调用)我希望这个Render方法覆盖Render类的render方法,但它不起作用。
package SimpleEngine.Level;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.glClear;
import SimpleEngine.Render.*;
import SimpleEngine.Primitives.*;
public class Level extends Render {
public void Render() {
glClear(GL_COLOR_BUFFER_BIT);
Primitives.DrawSquare(200, 200, 50, 50, 1, 0, 0);
}
}
我的主要方法(调用渲染但不能再使用)
package SimpleEngine;
import org.lwjgl.LWJGLException;
import SimpleEngine.Level.*;
import SimpleEngine.Logic.*;
import SimpleEngine.Input.*;
import SimpleEngine.Render.*;
import SimpleEngine.Entites.*;
import SimpleEngine.Timer.*;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
public class simpleEngine {
public static final int WIDTH = 640;
public static final int HEIGHT = 480;
private static boolean isRunning = true;
public static void main(String[] args) {
setUpDisplay();
setUpOpenGL();
Entity.setUpEntities();
Timer.setUpTimer();
while (isRunning) {
Render.Render();
Logic.logic(Timer.getDelta());
Input.input();
Display.update();
Display.sync(60);
if (Display.isCloseRequested()) {
isRunning = false;
}
}
Display.destroy();
System.exit(0);
}
private static void setUpDisplay() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.setTitle("SimpleEngine");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}
}
private static void setUpOpenGL() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
}
}
答案 0 :(得分:5)
您无法覆盖静态方法。
如果子类定义的类方法与超类中的类方法具有相同的签名,则子类中的方法会隐藏超类中的方法。
和
如果尝试将超类中的实例方法更改为子类中的类方法,则会出现编译时错误,反之亦然。
从static
类Render
方法移除render()
并将Level class Render
方法更改为render()
以引入覆盖行为
编辑:
while (isRunning) {
new Level().Render();
注意:Java命名约定建议方法名称应以小写字母开头,名称应为camelcase。
答案 1 :(得分:3)
您无法覆盖静态方法。
另外,请注意,因为Java是区分大小写的语言,因此Render()
与render()
不同,并且可能是完全不同的方法。
答案 2 :(得分:0)
我建议做一些更通用的东西,比如有一个界面,为你的游戏可能实现的任何状态定义常用方法,这实际上就像你前进的方向。这只会结合你的逻辑和渲染类。例如:
public Interface IGameState {
public void update(int delta);
public void render();
}
然后你可以有一个实现该接口的类,如:
public class InGameState implements IGameState {
public InGameState() { }
// This is similar to your Logic class
public void update(int delta) {
// Perform any updates necessary such as handling keyboard input
}
public void render() {
// What you included in your example
glClear(GL_COLOR_BUFFER_BIT);
Primitives.DrawSquare(200, 200, 50, 50, 1, 0, 0);
}
}
然后你的主要方法看起来像这样:
public static final int WIDTH = 640;
public static final int HEIGHT = 480;
private static boolean isRunning = true;
private IGameState currentGameState;
public static void main(String[] args) {
setUpDisplay();
setUpOpenGL();
Entity.setUpEntities();
Timer.setUpTimer();
// Create a new game state
currentGameState = new InGameState();
while (isRunning) {
// Change to this type of thing
// The delta would be the time since last frame so you can stay frame rate
// independent
currentGameState.update(delta);
currentGameState.render();
Display.update();
Display.sync(60);
if (Display.isCloseRequested()) {
isRunning = false;
}
}
Display.destroy();
System.exit(0);
}