Stránka 1 z 1

problém s kódem  Vyřešeno

Napsal: 01 bře 2015 12:59
od CallManyCZ
Zdravím... potřebuju poradit..
Našel jsem si na YT tutorial na Java Hru ....

V tom tutoriálu mu jde všechno v pořádku, ale já když tu aplikaci spustím vypíše mi to do konzole : 3800 FPS a 60 TICKS ale potom se mi sekne počítač a když kliknu a křížek abych to vypnul tak musím čekat cca 5 min než se ta apka vypne :/


tady je ten kód



Třída Window:

Kód: Vybrat vše

package cz.CallManyCZ.neon.window;

import java.awt.Dimension;

import javax.swing.JFrame;

public class Window {
   
   public Window(int w, int h, String title, Game game){
      game.setPreferredSize(new Dimension(w,h));
      game.setMaximumSize(new Dimension(w,h));
      game.setMinimumSize(new Dimension(w,h));   
      
      JFrame frame = new JFrame(title);
      frame.add(game);
      frame.pack();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      
      
      game.start();
   }
   
}



Třída Game :

Kód: Vybrat vše

package cz.CallManyCZ.neon.window;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

public class Game extends Canvas implements Runnable{

   private static final long serialVersionUID = 1L;
   
   private boolean running = false;
   private Thread thread;
   
   
   
   public synchronized void start(){
      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(){
      BufferStrategy bs = this.getBufferStrategy();
      
      if(bs == null){
         this.createBufferStrategy(3);
         return;
      }
      
      Graphics g = bs.getDrawGraphics();
      //////////////////////////////////
      
      
      
      
      g.setColor(Color.black);
      g.fillRect(0, 0, getWidth(), getHeight());
      
      //////////////////////////////////
      g.dispose();
      bs.show();
         
   }
   
   public static void main(String[] args){
      new Window(800, 600, "Neon Platform Game Prototype", new Game());
   }
   
}


nevím jestli to náhodou není nějaký bug BufferStrategy :/