我有以下类(片段),在我的render()
方法中,我正在使用BufferStrategy
进行缓冲。我遇到的问题是,当我移动图像时,它会留下一条尾巴。
我需要对代码进行什么处理才能使尾部不显示?这是代码:
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);
最佳答案
每次轮子移动时都需要刷新,然后将Canvas重新绘制为黑色,然后再次绘制轮子的位置。
关于java - Java游戏图像缓冲留下尾部,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24237389/