2016-03-23 17 views
0

我正在嘗試使用delta來移動矩形。但是,當我乘以速度並將其添加到矩形x位置時,該矩形不會移動。我認爲問題可能在於遊戲循環,但我不認爲這是事實。 下面的代碼:當乘以時間差時矩形不移動

package Main; 

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.image.BufferStrategy; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

//do double buffering 

public class Game extends Canvas { 
    int x, y; 
    private static final long serialVersionUID = 1L; 

    public static final int height = 400; 
    public static final int width = height * 16/9; 

    JPanel p; 
    Game game; 

    Image buffer; 

    KeyListener kl; 
    MouseListener ml; 

    public boolean running = true; 

    public Game(){ 
     kl = new KeyListener(){ 

      public void keyPressed(KeyEvent e) { 

      } 

      public void keyReleased(KeyEvent e) { 

      } 

      public void keyTyped(KeyEvent e) { 

      } 
     }; 

     ml = new MouseListener(){ 

      public void mousePressed(MouseEvent e) { 

      } 

      public void mouseReleased(MouseEvent e) { 

      } 
      public void mouseClicked(MouseEvent e) { 

      } 

      public void mouseEntered(MouseEvent e) { 

      } 

      public void mouseExited(MouseEvent e) { 

      } 
     }; 
    } 

    public void update(double delta){ 
     x += 10/1000 * delta; 
    } 

    public void render(){ 
     BufferStrategy bs = getBufferStrategy(); 
     if(bs==null){ 
      createBufferStrategy(2); 
      return; 
     } 
     Graphics g = bs.getDrawGraphics(); 
     g.clearRect(0, 0, getWidth(), getHeight()); 
     g.setColor(Color.BLACK); 
     g.fillRect(x, y, 100, 100); 

     g.dispose(); 
     bs.show(); 
    } 

    public void run(){ 

     //initialize time loop variables 
     long lastLoopTime = System.nanoTime(); 
     final int TARGET_FPS = 60; 
     final long OPTIMAL_TIME = 1000000000/TARGET_FPS; 
     double lastFpsTime = 0; 

     //Main game loop 
     while(running) 
     { 
      //Calculate since last update 
      long now = System.nanoTime(); 
      long updateLength = now - lastLoopTime; 
      lastLoopTime = now; 
      double delta = updateLength/((double)OPTIMAL_TIME); 

      //update frame counter 
      lastFpsTime += updateLength; 

      //update FPS counter 
      if(lastFpsTime >= 1000000000) 
      { 
       lastFpsTime = 0; 
      } 

      //game updates 
      game.update(delta); 

      //graphics (gameState) 
      game.render(); 

      try{ 
       Thread.sleep((Math.abs(lastLoopTime - System.nanoTime() + OPTIMAL_TIME)/1000000)); 
      }catch(Exception e){ 
       System.out.println("Error in sleep"); 
      } 
     } 
    } 

    public void start(){ 
     JFrame frame = new JFrame("Game"); 

     game = new Game(); 

     frame.add(game); 
     frame.pack(); 

     frame.setSize(width, height); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationRelativeTo(null); 

     frame.addKeyListener(kl); 
     frame.addMouseListener(ml); 

     frame.setVisible(true); 

     run(); 
    } 

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

回答

0

xy是整數,10/1000 * delta是大多數時間小於1 Incremeting x這樣的值不會改變x可言。

要解決這個問題,儘量的x的類型更改爲floatdouble,只是用Math.floor()或只是其轉換爲int繪製時:

g.fillRect((int) x, (int) y, 100, 100); 
0

要繼續在以前的答案:10/1000會給你零,而10./1000會給你它代表的實際數字。所以你應該改變你的陳述爲

x + =(int)(10.000 * delta);

,希望增量比1000/10更大一些號碼,以提高該整數大於0

0

你需要改變你的變量x和y是float或double。

double x, y; 

,如果你想看到的運動,你需要改變這樣的事情...

x += 100d/1000d * delta; 

你之前在做什麼是由一個整數除以整數10 1000結果爲0。

您還需要投的結果,整數,當你繪製矩形...

g.fillRect((int)x, (int)y, 100, 100);