以下代码不起作用。我正在尝试使用按键方法向左、向右、向上和向下移动玩家,但是当我按下按键时它没有响应。我没有粘贴整个代码,只是移动盒子的部分还有其他 if 语句来实现其他内容的移动。
public class innerClassKeyPressed {
void keyPressed( KeyEvent e)
{
int key= e.getKeyCode();
if(key==KeyEvent.VK_LEFT){
dx=-1;
}
if(key==KeyEvent.VK_RIGHT){
dx=1;
}
if (key==KeyEvent.VK_UP){
dy=-1;
}
if (key==KeyEvent.VK_DOWN){
dy=1;
}
if (key == 82)
{
initLevel(currlevel);
} //R
if (key == 78)
{
currlevel++;
initLevel(currlevel);
}
if ( (key == KeyEvent.VK_LEFT ) && ( key == KeyEvent.VK_RIGHT ) &&
(key == KeyEvent.VK_UP ) && ( key == KeyEvent.VK_DOWN )) {
return;
}
for (int row=0; row < myArray.length; row++)
{
for (int column=0; column < myArray[row].length; column++)
{
if( myArray[row][column]== Contents.PLAYER) {
if (myArray[row+dy][column+dx] == Contents.BOX)
{
if (myArray[row+dy*2][column+dx*2] == Contents.EMPTY)
{
myArray[row+dy][column+dx]= Contents.PLAYER;
myArray[row][column]= Contents.EMPTY;
myArray[row+dy*2][column+dx*2]= Contents.BOX;