[英]Moving Object with Keys Leaves a Trail
In my Java game, I have a player class which has a BufferedImage assigned to it from a sprite sheet. 在我的Java游戏中,我有一个玩家类,该类具有一个从Sprite表分配的BufferedImage。 I've just added in KeyAdapters and KeyListeners which just move the player around the screen.
我刚刚添加了KeyAdapters和KeyListeners,它们只是在屏幕上移动播放器。 However, when I do this, it leaves behind a trail of the image.
但是,当我这样做时,它留下了图像的痕迹。
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
p.render(g); //p is the player object
g.dispose();
bs.show();
}
public static void main(String[] args) {
Game game = new Game();
game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
JFrame frame = new JFrame(game.TITLE);
frame.add(game);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
game.start();
}
If I understand correctly, your issue might be that you have a background that seems like it keeps the last drawn things in it. 如果我理解正确,那么您的问题可能是您的背景看起来像是在保留最后绘制的内容。
So I think that you need to re-paint the background when you move the image .. 因此,我认为您在移动图像时需要重新绘制背景..
Here is a reference to Graphics basics in Java with simple exampels 这是通过简单的示例参考 Java中的Graphics基础
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.