0
我是一名初學者程序員,最近我開始嘗試使用Java進行遊戲。爲什麼不顯示跳躍動畫?
這是非常基本的,不包含任何類(儘管它應該),但無論如何,我試圖用一個JLabel
我的精靈使在JPanel
跳躍的動畫,但每當我嘗試時的每個動作之間的空間標籤使用Thread.sleep(millis)
Java似乎跳過它並將標籤移動到最後位置。
JFrame frame = new JFrame("malario");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(700, 700);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.blue);
JLabel malario = new JLabel("Malario");
malario.setOpaque(true);
malario.setBackground(Color.green);
panel.add(malario);
malario.setBounds(100, 550, 50, 50);
JLabel platform = new JLabel();
platform.setOpaque(true);
platform.setBounds(0,600,700,50);
panel.add(platform);
frame.setContentPane(panel);
frame.addKeyListener(new KeyListener() {
int originalx = 100;
int originaly = 550;
int currentlocx = originalx;
int currentlocy = originaly;
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
malario.setBounds(currentlocx+10,currentlocy , 50, 50);
currentlocx = currentlocx+10;
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
malario.setBounds(currentlocx-10,currentlocy , 50, 50);
currentlocx = currentlocx-10;
}
int jumpy=0;
if(e.getKeyCode()==KeyEvent.VK_UP){
jumpy= currentlocy-100;
while(jumpy!=currentlocy){
malario.setBounds(currentlocx,currentlocy-10 , 50, 50);
try {
Thread.sleep(1);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
currentlocy = currentlocy-10;
}
}
}
});
}
public static int Time(){
return (int)System.currentTimeMillis();
}
}