矩形在乘以增量时间时不移动

时间:2016-03-23 19:55:32

标签: java delta

我正在尝试使用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();
    }
}

3 个答案:

答案 0 :(得分:0)

xy是整数,10/1000 * delta大部分时间都小于1.使用此值增加x不会更改x所有

要解决此问题,请尝试将x的类型更改为floatdouble,然后使用Math.floor()或将其转换为int在绘制时:

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

答案 1 :(得分:0)

继续上一个答案:10/1000会给你零,而10./1000会给你它代表的实数。所以你应该把你的陈述改为

x + =(int)(10./1000 * delta);

并且希望delta是一个大于1000/10的数字,以将其提升为大于0的整数。

答案 2 :(得分:0)

您需要将x和y变量更改为float或double。

double x, y;

如果你希望看到动作,你需要改变这样的......

x += 100d / 1000d * delta;

之前您正在做的是将整数10除以整数1000并得到0作为结果。

当您绘制矩形时,您还需要将结果转换为整数...

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