0
我正在使用slick2d我用布爾來繪製庫存,如果我按「我」是真實的,所以它會繪製渲染方法,但我不能在第二次按下「我」時註冊它,我也試過一個計數器來獲取我被按下的次數,而且工作正常不太好。當我按下「我」時,我有一個庫存畫出,但我想知道如何擺脫它,如果我按「我」agian
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;
}
}
歡迎SO。請發佈您遇到問題的代碼。 – CConard96
爲什麼不使用布爾值來指示庫存狀態?然後你可以在每次按下時切換它。 「inv =!inv;」 – CConard96
感謝它的工作 – Kapperino