5

Java can't seem to find my constructor, I have no idea whats wrong. Is there a problem with having the throuws InterruptedException? Any Help would be appreciated, Thanks!

    package gameloop;

    import javax.swing.*;

    public class GameLoop extends JFrame {
        private boolean isRunning;
        public int drawx = 0;
        public int drawy = 0;

        public void GameLoop() throws InterruptedException{   
            setSize(700, 700);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);

            while(isRunning){
                doGameUpdate();
                render();
                Thread.sleep(1);
                if (isRunning){
                    GameLoop();
                }
            } 
        }

        private void doGameUpdate() {
            GameUpdate GU = new GameUpdate();
        }

        private void render() {
            Draw dr = new Draw();
        }

       public static void main(String[] args) {
            GameLoop GL = new GameLoop();
        }
    }
2
  • 6
    Remove the void. A constructor does not have a return type. Commented Sep 11, 2013 at 0:31
  • You should also move the update/render loop out of the constructor and into its own function, as well as remove the infinite recursion from the update/render loop. Commented Sep 11, 2013 at 15:55

4 Answers 4

6

A constructor is named exactly like its class, and has no return type. If you provide a return type, even void, you create a method called GameLoop. What you are looking for is

public GameLoop()

rather than

public void GameLoop()
Sign up to request clarification or add additional context in comments.

Comments

4

You need public GameLoop() constructors don't have return types

2 Comments

Thanks for the initial help! I got the window to show up, but now I'm getting new errors. In my Draw and Update Classes both of the line "public class draw extends GameLoop" and "public class GameUpdate".
best to create a new question
4

That's not a constructor - this is:

public GameLoop() throws InterruptedException

A constructor can not have a return type (void in your code), if you add one, Java will interpret it it as a normal method - even if it's called exactly like the class!

Comments

3

You constructor has a return type so it is treated as any other method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.