【发布时间】:2016-09-04 01:53:09
【问题描述】:
我有以下问题:我正在使用 LibGdx 库在 JAVA 中练习。我正在制作一个小型 2D 航天器射击游戏......我可以很好地移动我的精灵,并且在我不移动精灵时完美拍摄(如我所愿),但是当我移动精灵并尝试拍摄时问题就开始了。
我将让代码旁边的图片来说明我是如何处理镜头路径的......这是我定义镜头路径的激光功能,在某些方面,航天器不是边走边拍,找不到原因:
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)。
【问题讨论】:
-
您的代码在我看来是正确的。您能否提供任何有助于诊断问题的其他信息?
-
@AustinD 这就是我必须在船的机动性和射击方面展示的全部内容......我不明白为什么不在飞机的某些象限移动时射击(我注意到在我上传的图中)......我可以展示渲染代码之后的内容(与移动或射击无关的东西)