2014-08-28 78 views
-1
import java.awt.Canvas; 

public class GSM extends Canvas implements Runnable { 

//The game state manager 

public final long secondNS = 1000000000; 
public final long frameNS = secondNS/60; 
public boolean running = true; 
public long now = System.nanoTime(); 
public long startTime = now; 
public long lastFrame = now; 
public long lastSecond = now; 
public int frames = 0; 

public void run() 
{ 

    System.out.println("Program started."); 
    while(running) 
    { 
     now = System.nanoTime(); 
     if(now - lastFrame >= frameNS) 
     { 
      lastFrame = now; 
      frames++; 
     } 
     if(now - lastSecond >= secondNS) 
     { 
      lastSecond = now; 
      System.out.println(frames); 
      frames = 0; 
     } 
    } 
} 

public static void main(String[] args) 
{ 
    new GSM(); 
} 
} 

刚开始制作游戏引擎,但程序马上终止。有人可以指出错误吗?我知道有一些缺失,这将是超级明显的,我要facepalm,我感谢你的帮助。谢谢!程序在运行前终止?

+2

你真的需要从学习基础开始。即使是一个简单的游戏引擎也是一个复杂的软件,你还没有专业知识。从更简单的项目开始,继续前进。 – chrylis 2014-08-28 22:54:56

+1

你知道即使你创建了一个'Thread'来运行你的'Runnable',它不会在屏幕上显示任何GUI,因为它是一个'Canvas',对吧?而且,你正忙着在你的'while'循环中等待,这是一件坏事。 – 2014-08-28 23:08:35

回答

1

GSM实现Runnable,但你不能真正创建一个线程它在运行,因此run()方法永远不会被调用

+0

我该怎么做? – user2603506 2014-08-28 22:51:30

+0

Paul的回答有个例子 - 'new Thread(new GSM())。start();' – rdowell 2014-08-28 22:54:47

1

你正在创建一个新的GSM,但你永远不会调用运行。我假设你试图启动一个线程,但这需要你使用一个Runnable和一个线程对象。如果这是你想要做的Oracle has a tutorial on it