libgdx 从项目符号数组中删除项目符号



im 正在创建一个 2D 游戏。 我有一个子弹类数组,用于创建我的项目符号。 我还有另一个数组,它由精灵组成,它们在屏幕上移动一次,每 3 到 4 秒渲染一次。

问题:

当子弹

和其他角色在同一位置相遇时,子弹应被移除或删除,并且永远不会回来。

请帮帮我...

用于呈现项目符号的代码:

子弹.java:

import com.badlogic.gdx.math.Vector2;
public class Bullet{
public Vector2 bulletLocation = new Vector2(0, 0);
public Vector2 bulletVelocity = new Vector2(0, 0);
public Bullet(Vector2 sentLocation, Vector2 sentVelocity) {
    bulletLocation = new Vector2(sentLocation.x, sentLocation.y);
    bulletVelocity = new Vector2(sentVelocity.x, sentVelocity.y);
}
public void Update() {
    bulletLocation.x += bulletVelocity.x;
    bulletLocation.y += bulletVelocity.y;
}
}  

主类:

ArrayList<Bullet> bulletManager = new ArrayList<Bullet>();
Array<Other> OthersManager = new Array<Other>();
Bullet currentbullet;
Other currentOthers;

渲染();

int other = 0;
while (other < OthersManager.size) {
currentOthers = OthersManager.get(other);
        if (currentOthers.OtherLocation.x > guy.getX()) {
            currentOthers.OtherVelocity.x = -2f;
        }
        if (currentOthers.OtherLocation.x < guy.getX()) {
            currentOthers.OtherVelocity.x = 2f;
        }
        currentOthers.Update();
        others.setPosition(currentOthers.OtherLocation.x,currentOthers.OtherLocation.y);
        others.draw(batch);
        other++;
    }
    int counter = 0;
    while (counter < bulletManager.size()) {
        currentbullet = bulletManager.get(counter);
        currentbullet.Update();
        shoot.setPosition(currentbullet.bulletLocation.x,currentbullet.bulletLocation.y);
        if(currentOthers.OtherLocation.x == currentbullet.bulletLocation.x){
            bulletManager.remove(currentbullet);
        }
        shoot.draw(batch);
        counter++;
    }

似乎问题出在这个代码块上:

    if(currentOthers.OtherLocation.x == currentbullet.bulletLocation.x){
        bulletManager.remove(currentbullet);
    }

我建议您使用 Vector2 类中的 epsilonEquals 方法来检查您的当前 Others.OtherLocation 是否与 currentbullet.bulletLocation 匹配:

    if(currentOthers.OtherLocation.epsilonEquals(currentbullet.bulletLocation)){
        bulletManager.remove(currentbullet);
    }

此外,您可以将 epsilonEquals 与 float epsilon 一起使用:文档

最新更新