所以我有一个应用程序可以成功地向左、向右、向上或向下移动球(椭圆),具体取决于按下键盘的哪个按钮。但是,我无法让球以一定角度移动。我试图通过说如果用户持有 2 个方向键,球会斜着进入。例如,如果他们单击左键和上键,我希望球向西北方向移动。如果你们中的任何人看到导致问题的原因,我将非常感谢您的帮助!
引擎
class KeyClickListener implements KeyListener
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_DOWN )
{
component.moveCircle(1);
}
else if (e.getKeyCode() == KeyEvent.VK_UP)
{
component.moveCircle(2);
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
{
component.moveCircle(3);
}
else if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
component.moveCircle(5);
}
//this is my attempt at telling whether or not the user was holding two keys
else if (e.getKeyCode() == KeyEvent.VK_LEFT && e.getKeyCode() == KeyEvent.VK_UP)
{
component.moveCircle(5);
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
圆形组件
public void moveCircle(int w)
{
dir = w;
if (dir == 1)
{
y+=20;
repaint();
if (y>=560)
{
dir=2;
}
}
.
.
.
else if (dir == 5)
{
x-=10;
y-=10;
repaint();
if(x<=0 )
{
dir=3;
}
}
}