我正在创建一个2d游戏。我有一个bullet类的数组,我用它来创建我的bullet。我还有另一个数组,它由sprites组成,它在屏幕上移动,每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++;
}发布于 2016-01-29 16:16:16
看起来问题出在下面这段代码中:
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);
}此外,您可以将epsilonEquals与float epsilon一起使用:documentation
https://stackoverflow.com/questions/35063969
复制相似问题