简体   繁体   中英

Overiding a method wont work

I am trying to make my own engine but I need some help.

I am currently doing the level system. The level class extends the render class and the level class overides the Render classes render method. Finally the render class is called from the main class but I dont call the level class.

EDIT:

I have removed the static but now cannot call the render method. I know I am very nooby I kind of teach my self.

package SimpleEngine.Render;

Render Class (This is called)

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 (This is not called) I want this Render method to override the Render classes render method but it doesn't work.

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

    }

My main method (calls render but cannot anymore)

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

}

You can't override static methods.

As per Oracle tutorail

If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass.

AND

You will get a compile-time error if you attempt to change an instance method in the superclass to a class method in the subclass, and vice versa.

Remove static from Render class render() method and change Level class Render method to render() to introduce overriding behavior

EDIT:

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

Note: Java naming convention suggests that method name should start with small letter and name should be camelcase.

You can't override a static method.

Also, be careful because Java is a case sensitive language, so Render() is not the same as render() , and could be a completely different method.

I would recommend doing something more generic such as having an interface which defines common methods for any state your game could be in which actually looks like the direction you're heading in. This would just combine your Logic and Render classes. For example:

public Interface IGameState {

    public void update(int delta);

    public void render();
}

And then you can have a class that implements that interface such as:

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

}

And then your main method could look something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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