因此,我已经创建了这个很小的(用于检测碰撞的)(效率不高,但这是我第一次编写游戏...)系统。当涉及到基本检测时,例如在子弹和建筑物之间或在玩家和NPC之间,它可以工作。
但是经过一些测试,我发现Bullet实体仅在移动时就无法与士兵(NPC)实体发生碰撞。
子弹:
public class soldier_nightops extends Enemy {
private int xspeed = -212;
private Animation animRight, animLeft;
public soldier_nightops(Handler handler, float x, float y) {
super(handler, x, y, 64, 64);
bounds.x = 15;
bounds.y = 15;
bounds.width = xspeed;
bounds.height = 2;
animRight = new Animation(180, Assets.soldier_nightops_walkright);
animLeft = new Animation(180, Assets.soldier_nightops_walkleft);
setEnemyXMove(100);
setEnemyYMove(200);
setType(2);
}
public void tick() {
// reminder: check if entity still exists... for later stages
// Animations
animRight.tick();
animLeft.tick();
moveBot();
//THE MOVEMENT OF MY BOT IS WHAT MAKES HIM UNHITTABLE!!!!!!
// move();
}
boolean moveleft = true;
private void moveBot() {
if (moveleft == true) {
x -= 2;
setEnemyXMove(getEnemyXMove() - 2);
if (getEnemyXMove() <= 0) {
moveleft = false;
bounds.x = 40;
bounds.width = -xspeed;
setEnemyXMove(200);
}
} else {
x += 2;
setEnemyYMove(getEnemyYMove() - 2);
if (getEnemyYMove() <= 0) {
bounds.width = xspeed;
moveleft = true;
bounds.x = 15;
setEnemyYMove(200);
}
}
}
public void render(Graphics g) {
// reminder to change x and y...
g.drawImage(getCurrentAnimationFrame(), (int) (x - handler.getGameCamera().getxOffset()),
(int) (y - handler.getGameCamera().getyOffset()), width, height, null);
g.setColor(Color.red);
g.fillRect((int) (x + bounds.x - handler.getGameCamera().getxOffset()),
(int) (y + bounds.y - handler.getGameCamera().getyOffset()), bounds.width, bounds.height);
}
private BufferedImage getCurrentAnimationFrame() {
if (moveleft == true) {
return animLeft.getCurrentFrame();
} else if (moveleft == false) {
return animRight.getCurrentFrame();
}
return animRight.getCurrentFrame();
}
}
士兵:
{{1}}
我已经进行了测试,并得出结论,只要moveBot();方法未实现,一切正常。但是,如何使它与MoveBot()方法一起使用? 预先感谢!
问题的演示:https://streamable.com/a7lkz
p.s,整个bounds.x和-speedx游戏都是一个矩形框,用于检测玩家并告诉士兵何时射击。这是供以后使用的。