では、まず始めに。System.out.println(e); を実行すると、キーを押すと印刷されます。しかし、私はこれをintに格納する方法を一生理解できません。int press = e.KEY_PRESSED(); を実行しても、IDE でエラーが発生しません。または int 押された = e.getKeyCode(); しかし、プレスを印刷しようとしても何も起こりません。
私はこれを何時間も機能させようとしてきましたが、Google KeyEvent ハンドラーと Javadocs は、これに関してはほとんど役に立たないようです。
public void keyPressed(KeyEvent e) {
pressed = e.getKeyCode();
System.out.println(pressed);
}
do{
time = System.currentTimeMillis();
do{
if(pressed == 37||pressed==38||pressed==39||pressed==40){
lastvalid=pressed;
}
}
while(System.currentTimeMillis() < time + speed);
switch(lastvalid){
case 37: catarloc.set(0, (Integer)catarloc.get(0)-1); break;
case 38: catarloc.set(1, (Integer)catarloc.get(1)-1); break;
case 39: catarloc.set(0, (Integer)catarloc.get(0)+1); break;
case 40: catarloc.set(1, (Integer)catarloc.get(1)+1); break;
}
if(Math.random() > .95 || apples < 1){
applearray[(int)(Math.random()*100/2.8)][(int)(Math.random()*100/4)] = true;
apples++;
}
score+=catarloc.size()-1;
label.setText("Score "+ score);
mainWindow.repaint();
}
while(win == false || lose == false);