简体   繁体   中英

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

I'm using slick2d I used a Boolean to draw the inventory, if I pressed "I" its true so it will graph on the render method, but I can't get it to register the second time I pressed "i", I also tried a counter to get how many times I is pressed, and that worked but not that well.

    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;
    }

}

You can use a boolean instead of an int. Then, each time the key "I" is pressed, you can toggle it's value.

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

Created answer from my original comment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM