当我按 "i" 时,我有一个库存被抽出,但我想知道如果我按 "i" agian,如何摆脱它

I have an inventory drawn out when i press "i" but I want to know how to get rid of it if i press "i" agian

我正在使用 slick2d 我使用了一个布尔值来绘制库存,如果我按下 "I" 它是真的所以它会在渲染方法上绘制图形,但我无法让它第二次注册我按下了 "i",我还尝试了一个计数器来计算我被按下的次数,这很有效,但效果不佳。

    import org.newdawn.slick.*;
    import org.newdawn.slick.state.*;

public class Play extends BasicGameState{
    Animation player,moveup,movedown,moveleft,moveright;
    Image map,bk;
    Inventory kappa;
    boolean quit = false;
    int[] duration ={200,200,200};
    float posX=0;
    float posY=0;
    float shiftX=posX+640;
    float shiftY =posY+360;
    float inX=0;
    float inY=0;
    Image[] walkUp = new Image[3];
    Image[] walkDown = new Image[3];
    Image[] walkLeft = new Image[3];
    Image[] walkRight = new Image[3];
    PotionLibraray pl;
    int inv = 0;
    public Play(int state){

    }
    public void init(GameContainer gc,StateBasedGame sbg)throws SlickException
    {
        map = new Image("res/map.png");
        bk=new Image("res/BlueKnight.png");
        kappa = new Inventory();
        pl = new PotionLibraray();
        for(int x=0;x<9;x++)
        kappa.add(pl.shpp);

        for(int x=0;x<3;x++)
        {
        walkDown[x]=bk.getSubImage(x*32, 0, 32, 32);
        }
        for(int x=0;x<3;x++)
        {
        walkLeft[x]=bk.getSubImage(x*32, 32, 32, 32);
        }
        for(int x=0;x<3;x++)
        {
        walkRight[x]=bk.getSubImage(x*32, 64, 32, 32);
        }
        for(int x=0;x<3;x++)
        {
        walkUp[x]=bk.getSubImage(x*32, 96, 32, 32);
        }
        moveup = new Animation(walkUp,duration,false);
        movedown = new Animation(walkDown,duration,false);
        moveleft = new Animation(walkLeft,duration,false);
        moveright = new Animation(walkRight,duration,false);
        player =movedown;


    }
    public void render(GameContainer gc,StateBasedGame sbg,Graphics g)throws SlickException
    {

      map.draw(posX,posY);
      player.draw(shiftX,shiftY);
      g.drawString("player x:  "+posX+"\nplayer y: "+posY, 1000, 50);
      if(inv%2==1||inv==1)
      {
          kappa.draw(300, 400);
      }

    }
    public void update(GameContainer gc,StateBasedGame sbg,int delta)throws SlickException
    {
        Input input = gc.getInput();
        if(input.isKeyDown(Input.KEY_I))
        {
            inv++;

        }




        if(input.isKeyDown(Input.KEY_W))
        {
            player=moveup;
            posY+=delta*0.3f;

        }
        if(input.isKeyDown(Input.KEY_S))
        {
            player=movedown;
            posY-=delta*0.3f;

        }
        if(input.isKeyDown(Input.KEY_A))
        {
            player=moveleft;
            posX+=delta*0.3f;

        }
        if(input.isKeyDown(Input.KEY_D))
        {
            player=moveright;
            posX-=delta*0.3f;

        }

    }


    public int getID() {

        return 1;
    }

}

您可以使用布尔值而不是整数。然后,每次按下 "I" 键,您都可以切换它的值。

if (input.isKeyDown(Input.KEY_I))
{
    inv = !inv;
}

根据我的原始评论创建了答案