我试图让我的碰撞矩形起作用。为了调试一些问题,我一直在打印位置getX()和getY()来确定碰撞盒的位置。但由于某些原因,它似乎没有更新(不断说0.0,即使子弹射击)。
这是Bullet构造函数,我最初:
public Bullet(float spriteX, float spriteY, int playerDirection){
this.position = new Vector2(spriteX, spriteY);
this.bulletDirection = Player.playerDirection;
try{
img = new Texture(Gdx.files.internal("bullet-green-icon.png"));
sprite = new Sprite(img);
} catch (Exception e){
e.printStackTrace();
}
collisionBoxBullet = sprite.getBoundingRectangle();
System.out.println("collision bullet box: x" +collisionBoxBullet.getX());
System.out.println("collision bullet box: y" +collisionBoxBullet.getY());
}
这两条打印线的评估值为0.0会让我感到困惑,因为子弹坐标应该基于精灵的坐标。在玩家类中调用它:
else if (gip.keyDown(Keys.SPACE)){
Bullet bullet = new Bullet(sprite.getX(), sprite.getY(), playerDirection);
Main.currentBullets.add(bullet);
}
据我所知,我正在创建子弹,创建子弹精灵,围绕它包裹一个矩形。那么为什么它在0.0 / 0.0处显示它的矩形坐标,而精灵在250/150处。
这里我每次子弹移动时都会更新矩形(子弹在屏幕上移动)。
public static void update(float dt, ArrayList<Bullet> bullets, SpriteBatch batch){
for (int i = 0; i < bullets.size(); i++){
if (bullets.get(i).bulletDirection == 1){
bullets.get(i).position.y += bullets.get(i).velocity * dt;
bullets.get(i).collisionBoxBullet = bullets.get(i).sprite.getBoundingRectangle();
batch.draw(bullets.get(i).sprite, bullets.get(i).position.x,
bullets.get(i).position.y);
}
else if (bullets.get(i).bulletDirection == 2){
bullets.get(i).position.y -= bullets.get(i).velocity * dt;
batch.draw(bullets.get(i).sprite, bullets.get(i).position.x,
bullets.get(i).position.y);
bullets.get(i).collisionBoxBullet = bullets.get(i).sprite.getBoundingRectangle();
}
else if (bullets.get(i).bulletDirection == 3){
bullets.get(i).position.x -= bullets.get(i).velocity * dt;
batch.draw(bullets.get(i).sprite, bullets.get(i).position.x,
bullets.get(i).position.y);
bullets.get(i).collisionBoxBullet = bullets.get(i).sprite.getBoundingRectangle();
}
else if (bullets.get(i).bulletDirection == 4){
bullets.get(i).position.x += bullets.get(i).velocity * dt;
batch.draw(bullets.get(i).sprite, bullets.get(i).position.x,
bullets.get(i).position.y);
bullets.get(i).collisionBoxBullet = bullets.get(i).sprite.getBoundingRectangle();
}
从我在这里可以看出,精灵被绘制到一个新位置,然后再次围绕它生成一个矩形。为什么它一直说它在0.0 / 0.0?