目前我在我的程序中使用双缓冲(尽管你应该知道),并且我有一个存储 BufferedImages 和 Integers 的数组列表。目前我在屏幕上移动整个数组列表的代码是:
public static void MoveMap(final double ammount){
Thread t = new Thread(){
public void run(){
for(int i = 0; i < TileHelper.MAIN_LIST.size();i++){
TileHelper.MAIN_LIST.get(i).changeX(ammount*DATA.speed);
}
}
};
t.start();
}
这可以完成工作,但看起来并不顺利。即使我使用 0.25 之类的数字作为可变数量。(我只是注意到我在代码中拼写错误:O)如果我的所有图像都在这个数组中,我如何做出漂亮的平滑运动?顺便说一句,如果您想知道这有什么不顺利,这是因为它滞后并且在每张图像之间您会看到一个很大的空白区域。感谢您提供任何帮助...谢谢!
渲染代码:
public class Main extends JFrame {
public static int worldsize = 1080;
private static final long serialVersionUID = 6149918602875295087L;
public static void main(String args[]){
new Main();
}
TileHelper tiles = new TileHelper(TileHelper.MAIN_LIST);
public Main(){
DATA_IMAGES.initImages();
addDefaultBlocks();
setSize(640,480);
setResizable(false);
setTitle("Stranded");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
createBufferStrategy(2);
GameLoop();
addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT:
Movement.MoveMap(3.25);
break;
case KeyEvent.VK_RIGHT:
Movement.MoveMap(-3.25);
break;
case KeyEvent.VK_UP:
DATA.speed++;
break;
case KeyEvent.VK_DOWN:
DATA.speed--;
break;
case KeyEvent.VK_SPACE:
System.out.println(DATA.speed);
break;
}
}
});
}
private void addDefaultBlocks() {
for(int x = -1000/32; x < worldsize/32; x++){
for (int y = -1000/32; y < worldsize/32; y++){
TileHelper.addBlock(DATA_IMAGES.GRASS, x*32, y*32);
}
}
}
public void GameLoop(){
Thread gameloop = new Thread(){
public void run(){
while(true){
RenderInit();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
gameloop.start();
}
public void RenderInit(){
BufferStrategy buff = getBufferStrategy();
Graphics g = buff.getDrawGraphics();
g.clearRect(0, 0, getWidth(), getHeight());
Render(g);
g.dispose();
buff.show();
Toolkit.getDefaultToolkit().sync();
}
public void Render(Graphics g){
tiles.RenderAll(g);
}
}