我有两个静止不动的精灵,两个行走在GIF中。现在,问题在于,如果释放移动键,它将walkin(启动gif的布尔值)设置为false。这是有意的,但是如果您按住两个方向并放开一个方向,则精灵仍会在屏幕上移动,但是步入为假,并且GIF无法播放。
我已经尝试说过(e.KeyCode()!= KeyEvent.VK_W || ...)对于所有发行版而言都是如此,但这是行不通的。
public void getDirection()
{
if (dir == 2 && walkin == false)
{
ImageIcon sergei = new ImageIcon("src/sprites/L_Sergei_no-pack_1.gif");
image = sergei.getImage();
}
if (dir == 1 && walkin == false)
{
ImageIcon sergei2 = new ImageIcon("src/sprites/R_Sergei_no-pack_1.gif");
image = sergei2.getImage();
}
if (dir == 1 && walkin == true)
{
ImageIcon sergei2 = new ImageIcon("src/sprites/Sergei_Right_no-pack.gif");
image = sergei2.getImage();
}
if (dir == 2 && walkin == true)
{
ImageIcon sergei = new ImageIcon("src/sprites/Sergei_Left_no-pack.gif");
image = sergei.getImage();
}
w = image.getWidth(null);
h = image.getHeight(null);
}
...(将内容返回其他类的代码,包括x + = dx; y + = dy;)
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A)
{
dx = -2;
dir = 2;
walkin = true;
getDirection();
}
if (key == KeyEvent.VK_D)
{
dx = 2;
dir = 1;
walkin = true;
getDirection();
}
if (key == KeyEvent.VK_W)
{
dy = -2;
walkin = true;
getDirection();
}
if (key == KeyEvent.VK_S)
{
dy = 2;
walkin = true;
getDirection();
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A)
{
dx = 0;
walkin = false;
getDirection();
}
if (key == KeyEvent.VK_D)
{
dx = 0;
walkin = false;
getDirection();
}
if (key == KeyEvent.VK_W)
{
dy = 0;
walkin = false;
getDirection();
}
if (key == KeyEvent.VK_S)
{
dy = 0;
walkin = false;
getDirection();
}
}
到目前为止,如果您释放一个微调按钮,则精灵只会冻结动画,我希望它继续前进。