2016-11-27 12 views
0

現在、自分のコードで生成されているティックの量に問題があります。私は現在、ビデオゲームのガイドに従っており、ロジックを更新するために使用されるティックの量は、両方が60に安置されるべきフレームよりも指数関数的に高いです。これはYouTubeのビデオです https://www.youtube.com/watch?v=VE7ezYCTPe4&t=1358s 助言をいただければ幸いです。先進的でありがとう!あなたのwhile(delta >= 1){ループではJavaコードでこれらの数値が制限されないのはなぜですか?

package game; 

import java.awt.BorderLayout; 
import java.awt.Canvas; 
import java.awt.Dimension; 

import javax.swing.JFrame; 

public class game extends Canvas implements Runnable { 

private static final long serialVersionUID = 1L; 

public static final int WIDTH = 160; 
public static final int HEIGHT = WIDTH/12 * 9; 
public static final int SCALE = 3; 
public static final String NAME = "Game"; 

private JFrame frame; 

public boolean running = false; 

public game() { 
    setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 
    setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 
    setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 

    frame = new JFrame(NAME); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLayout(new BorderLayout()); 

    frame.add(this,BorderLayout.CENTER); 
    frame.pack(); 

    frame.setResizable(false); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
} 

public synchronized void start() { 
    running = true; 
    new Thread(this).start(); 

} 

public synchronized void stop() { 
    running = false; 

} 

public void run() { 
    long lastTime = System.nanoTime(); 
    double nsPerTick = 1000000000/60D; 

    int ticks = 0; 
    int frames = 0; 

    long lastTimer = System.currentTimeMillis(); 
    double delta = 0; 

    while (running) { 
     long now = System.nanoTime(); 
     delta += (now - lastTimer)/nsPerTick; 
     lastTime = now; 
     boolean shouldRender = false; 

     while(delta >= 1){ 
      ticks++; 
      tick(); 
      render(); 
      delta -= 1; 
      shouldRender = true; 
     } 

     if (shouldRender) { 
      frames++; 
      render(); 
      } 

     try { 
     Thread.sleep(2); 
    } catch(InterruptedException e) { 
     e.printStackTrace(); 
    } 

     if (System.currentTimeMillis() - lastTimer >= 1000){ 
      lastTimer += 1000; 
      System.out.println(ticks + " ticks, " + frames + " frames"); 
      frames = 0; 
      ticks = 0; 
     } 
    } 


} 

public void tick() { 
    //updates the logic of the game. 

} 

public void render() { 
    //prints out what the logic says to print out 
} 

public static void main(String[] args){ 
    new game().start(); 
} 

}

答えて

0

、あなたが実際にダニをインクリメント。そのデルタは完全なシステムであるため、約16666667nsでなければならないため、潜在的にデルタあたり16666667ダニがあります。あなたのチュートリアルは非常に興味があります。なぜなら一般的には、ループ内ではなく物理系のようなシステムでデルタを使うからです。ダニは1つのゲームループとみなされ、デルタは物理計算などに使用されます。Javaゲーム開発に関するリソースが必要な場合は、https://www.lwjgl.org/をチェックアウトすることを強くお勧めします。トピック。

関連する問題