0

Im am fairly new to Java as well as at making a game loop and am trying to create space invaders. However, when I execute the code the start() method does not run. why is this not happening? Is there other things I could change too to make it run better?

import java.awt.Canvas;
import javax.swing.*;

public class Main extends Canvas implements Runnable
{
  private boolean running = false;
  private Thread thread;

  public static void main(String[] args) {
    Window w = new Window(800,600,"Space Invaders", new Main());
  }

  public synchronized void start(){
    System.out.println("debug1");
    if(running)
    {
      return;
    }
    running = true;
    thread = new Thread(this);
    thread.start();
  }

  public void run()
  {
    long lastTime = System.nanoTime();
    double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;
    long timer =System.currentTimeMillis();
    int updates = 0;
    int frames = 0;
    while(running){
      long now = System.nanoTime();
      delta += (now - lastTime) / ns;
      lastTime = now;
       while(delta >=1){
        tick();
        updates++;
        delta--;
      }
      render();
      frames++;

      if(System.currentTimeMillis()- timer > 1000){
        timer += 1000;
        System.out.println("FPS: " + frames + "TICKS: " + updates);
        frames = 0;
        updates = 0;
      }
    }
  }

  private void tick()
  {

  }

  private void render()
  {

  }
}

1 Answers1

0

Add an explicit Constructor to your Main Class and call your start function.

public Main(){
   start();
}