[英]Can't shoot while moving in specific degrees (or rotations values) JAVA LibGdx
我有以下問題:我正在使用LibGdx庫在JAVA中進行練習。 我正在做一個2D太空飛船射擊游戲...我可以很好地移動精靈,並且在不移動精靈的情況下射擊完美(如我所願),但是當我移動精靈並嘗試射擊時,問題就開始了。
我將讓圖片旁邊的代碼來說明我是如何進行射擊路徑的...這是Laser函數,可在其中定義射擊路徑,在某些情況下,航天器在移動時不會射擊,但我找不到原因:
public Laser shoot(float grados, float x, float y){
Laser obj = new Laser();
if(grados == 0){ //Ok
obj.setVelocidadX(500);
obj.setVelocidadY(0);
obj.setRotacion(grados);
obj.setPosition(x + 80, y + 45);
}
if (grados == 90){ //Ok
obj.setVelocidadX(0);
obj.setVelocidadY(500);
obj.setRotacion(grados);
obj.setPosition(x + 55, y + 80);
}
if (grados == 180){ //Ok
obj.setVelocidadX(-500);
obj.setVelocidadY(0);
obj.setRotacion(grados);
obj.setPosition(x + 10, y + 55);
}
if(grados == -90){ //Ok
obj.setVelocidadX(0);
obj.setVelocidadY(-500);
obj.setRotacion(grados);
obj.setPosition(x + 45,y + 10);
}
if(grados > 0 && grados < 90){ //OK
obj.setVelocidadX(500);
obj.setVelocidadY(500);
obj.setRotacion(grados);
obj.setPosition(x + 75, y + 70);
}
if(grados > 90 && grados < 180){ //The spaceship not shoot
obj.setVelocidadX(-500);
obj.setVelocidadY(500);
obj.setRotacion(grados);
obj.setPosition(x + 25 , y + 80);
}
if(grados > -180 && grados < -90){ //The spaceship not shoot
obj.setVelocidadX(-500);
obj.setVelocidadY(-500);
obj.setRotacion(grados);
obj.setPosition(x + 20, y + 25);
}
if(grados > -90 && grados < 0){ //The spaceship not shoot
obj.setVelocidadX(500);
obj.setVelocidadY(-500);
obj.setRotacion(grados);
obj.setPosition(x + 75, y + 15);
}
return obj;
}
渲染航天器的代碼:
float dt = Gdx.graphics.getDeltaTime();
fireDelay -= dt;
nave.velocidadX = 0;
nave.velocidadY = 0;
if(Gdx.input.isKeyPressed(Keys.SPACE)){
if(fireDelay <= 0){
escenario.addActor(nave.shoot(nave.getRotation(),nave.getX(),nave.getY()));
fireDelay = 0.3f;
}
}
if(nave.velocidadX == 0 && nave.velocidadY == 0){nave.setAnimation(detenido);}
if(Gdx.input.isKeyPressed(Keys.LEFT)){
nave.velocidadX -= 200;
nave.setAnimation(corriendo);
}
if(Gdx.input.isKeyPressed(Keys.RIGHT)){
nave.velocidadX += 200;
nave.setAnimation(corriendo);
}
if(Gdx.input.isKeyPressed(Keys.UP)){
nave.velocidadY += 200;
nave.setAnimation(corriendo);
}
if(Gdx.input.isKeyPressed(Keys.DOWN)){
nave.velocidadY -= 200;
nave.setAnimation(corriendo);
}
我希望這艘船在移動時向四面八方開火...
更新:我發現了一個新問題,我無法一次按下紅色象限中的三個鍵。 我只能在第一個象限(0到90)中。
我發現了問題所在。 事實證明,我的代碼是正確的...問題? ...這是我的鍵盤。 看來我的鍵盤不支持同時按下某些組合鍵...我不得不進入下一頁來測試哪些組合有效:
https://www.microsoft.com/appliedsciences/keyboardghostingdemo.mspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.