【问题标题】:Start() Method not running- JavaStart() 方法未运行 - Java
【发布时间】:2021-08-13 17:28:32
【问题描述】:

我对 Java 以及制作游戏循环还很陌生,并且正在尝试创建太空入侵者。但是,当我执行代码时, start() 方法不会运行。为什么这没有发生?还有其他我可以更改的东西以使其运行得更好吗?

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()
  {

  }
}

【问题讨论】:

  • 您似乎没有在任何地方调用您的 start 方法。你为什么期望它运行?

标签: java game-loop


【解决方案1】:

将显式构造函数添加到您的 Main 类并调用您的 start 函数。

public Main(){
   start();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 2018-04-12
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多