繁体   English   中英

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

[英]libgdx remove bullet from bullet array

即时通讯创建2D游戏。 我有一个用于创建子弹的子弹类数组。 我还有另一个由精灵组成的数组,它们每3到4秒在屏幕上移动一次。

问题:

当子弹和其他精灵在同一位置相遇时,应将子弹移走或删除,并且永远不要回来。

请帮我...

呈现项目符号的代码:

bullet.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;

render();

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方法来检查您的currentOthers.OtherLocation是否与currentbullet.bulletLocation相匹配:

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

此外,您可以使用epsilon等于float epsilon: 文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM