Java游戏图像缓冲离开尾巴

时间:2014-06-16 05:59:47

标签: java graphics buffer bufferstrategy

我有以下类(Snippet),在我的render()方法中,我正在使用BufferStrategy进行缓冲。我遇到的问题是当我移动图像时,它会留下一个尾巴。

Example with tail

我需要对我的代码做什么才能使尾部无法显示?这是代码:

public class Main extends JFrame implements Runnable{

    private BufferStrategy bufferStrategy;

    public synchronized void start(){
        Thread thread = new Thread(this);
        thread.start();
    }

    public void run(){
        // Main Game Loop
        this.render();
        // End Main Game Loop
    }

    protected void render(){
        if(bufferStrategy == null){
            this.createBufferStrategy(3);
            bufferStrategy = this.getBufferStrategy();
        }
        Graphics g = bufferStrategy.getDrawGraphics();
        // Loop through a list of items to draw
        for(GameObject go : gameObjects){
            Image sprite = go.getComponent(SpriteRenderer.class).getSprite();
            Vector2 pos = go.getComponent(Transform.class).getPosition();
            g.drawImage(sprite, (int)pos.x, (int)pos.y, this);
        }
        g.dispose();
        bufferStrategy.show();
        Toolkit.getDefaultToolkit().sync();
    }
}

修改

我明白了:

Graphics g = bufferStrategy.getDrawGraphics();
super.paint(g);

1 个答案:

答案 0 :(得分:1)

每次车轮移动时都需要刷新,并在再次绘制车轮位置之前将画布重新涂成黑色。