首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >libgdx从项目符号数组中删除项目符号

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

Stack Overflow用户
提问于 2016-01-28 22:18:32
回答 1查看 155关注 0票数 0

我正在创建一个2d游戏。我有一个bullet类的数组,我用它来创建我的bullet。我还有另一个数组,它由sprites组成,它在屏幕上移动,每3到4秒渲染一次。

问题:

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

请帮帮我。

渲染项目符号的代码:

bullet.java:

代码语言:javascript
运行
复制
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;
}
}  

主类:

代码语言:javascript
运行
复制
ArrayList<Bullet> bulletManager = new ArrayList<Bullet>();
Array<Other> OthersManager = new Array<Other>();
Bullet currentbullet;
Other currentOthers;

render();

代码语言:javascript
运行
复制
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++;
    }
EN

回答 1

Stack Overflow用户

发布于 2016-01-29 16:16:16

看起来问题出在下面这段代码中:

代码语言:javascript
运行
复制
    if(currentOthers.OtherLocation.x == currentbullet.bulletLocation.x){
        bulletManager.remove(currentbullet);
    }

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

代码语言:javascript
运行
复制
    if(currentOthers.OtherLocation.epsilonEquals(currentbullet.bulletLocation)){
        bulletManager.remove(currentbullet);
    }

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35063969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档