2013-01-11 19 views
1

我努力使自己的引擎,但我需要一些幫助。Overiding的方法不會工作

我目前做級系統。 level類擴展了渲染類,而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() { 

    } 

} 

水平等級(這不叫)我想這個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); 
    } 

} 

回答

5

不能覆蓋靜態方法。

作爲每Oracle tutorail

如果子類定義了一類方法具有相同簽名作爲在超類中的一類方法中,在子類中的方法隱藏了一個超類中。

,如果你試圖在子類中改變超類的實例方法一類方法,你會得到一個編譯時錯誤,反之亦然。

刪除staticRenderrender()方法和改革問題高級別類Render方法render()介紹壓倒一切的行爲

編輯:

while (isRunning) { 
      new Level().Render(); 

注:Java的命名慣例表明,方法名的開頭爲小信和名字應該是camelcase。

+0

感謝幫助,但我刪除了靜態,現在我無法從我的主類調用Render方法。 – user1971189

+0

@ user1971189:請用最新代碼更新問題(包括主要方法)。 – kosa

+0

@Nambari由於'render()'不是靜態的,你必須創建一個** Level **對象,然後調用它的方法。 – Flawyte

3

不能覆蓋靜態方法。

另外,要小心,因爲Java是區分大小寫的語言,因此Render()render()不同,並且可能是完全不同的方法。

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); 
}