扩展矩形的类不会与libgdx重叠

时间:2015-11-06 16:29:19

标签: java libgdx

我试图删除子弹和敌人的子弹重叠敌人,但由于某种原因,没有检测到重叠。我不知道如何帮助它。 至于我,它假设正常工作

BULLET AND ENEMY CLASSES

private class Enemy extends Rectangle {
        public int health;
        public float x;
        public float y;
        public float speed;

        public Enemy (float x, float y, float speed, int health){
            this.x = x;
            this.y = y;
        }
        public void updateCoor(){
            x +=speed*Gdx.graphics.getDeltaTime();
            y = 100;//}
        }
    }

    private class Bullet extends Rectangle{
        public float x;
        public float y;
        public float speedX;
        public float speedY;

        public Bullet(float x, float y, float dirX, float dirY) {
            this.x = x;
            this.y = y;

            int factor = 2;
            float dividerFactor = Gdx.graphics.getWidth();
            speedX = factor*((dirX - x) / dividerFactor);
            speedY = factor*((dirY - y) / dividerFactor);
            System.out.println(TAG + " " + x + " " + y + " " + dirX + " " + dirY);
        }
        public void updateCoor(){
            x += speedX * Gdx.graphics.getDeltaTime() * 5000;
            y += speedY * Gdx.graphics.getDeltaTime() * 5000;
        }
    }

    SpriteBatch batch;
    Texture enemyImage,bulletImage;
    OrthographicCamera camera;
    Array<Enemy> enemies;
    Array<Bullet> bullets;
    long time;
    Vector2 vector2;
    Vector3 touchPos;
    int y;
    ShapeRenderer renderer;
    long lastEnemyTime;

    @Override
    public void create() {
        batch = new SpriteBatch();
        camera = new OrthographicCamera();

        camera.setToOrtho(false);

        enemyImage = new Texture(Gdx.files.internal("enemy.png"));
        bulletImage = new Texture(Gdx.files.internal("bullet.png"));
        time = TimeUtils.nanoTime();
        renderer = new ShapeRenderer();

        enemies = new Array<Enemy>();
        bullets = new Array<Bullet>();

        vector2 = new Vector2();
        touchPos = new Vector3();
       spawnEnemy();
        Gdx.input.setInputProcessor(this);
    }

产生敌人

    public void spawnEnemy() {
        Enemy enemy = new Enemy(0,20,10,2);
        lastEnemyTime =TimeUtils.nanoTime();
        enemy.height = 3;
        enemy.width = 6;
        enemy.speed = 100;
        enemies.add(enemy);
    }

产生子弹

    public void spawnBullet(int screenX, int screenY) {
        Bullet bullet = new Bullet(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/3+40, screenX, screenY);
        bullet.height = 1;
        bullet.width = 1;
        bullets.add(bullet);
    }
    @Override

呈现

    public void render() {
        Gdx.gl.glClearColor(0, 1, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();

        batch.begin();

        for (Bullet bullet : bullets) {
            batch.draw(bulletImage, bullet.x, bullet.y);
        }
        for (Enemy enemy: enemies){
            batch.draw(enemyImage,enemy.x,enemy.y);
        }

        batch.end();

        if (TimeUtils.nanoTime() - lastEnemyTime > 1000000000) {spawnEnemy();}

        Iterator<Enemy> iterator = enemies.iterator();
        while (iterator.hasNext()){
            Enemy enemy = iterator.next();
            enemy.updateCoor();
        }
        Iterator<Bullet> iter = bullets.iterator();
        while (iter.hasNext()) {
            Bullet bullet = iter.next();
            bullet.updateCoor();

            if (iterator.hasNext()){
                Enemy enemy = iterator.next();
            if (bullet.overlaps(enemy)) {
                iter.remove();
                iterator.remove();
                System.out.println("sdla");
            }
            }
        }
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        spawnBullet(screenX, Gdx.graphics.getHeight() - screenY);
        return true;
    }

2 个答案:

答案 0 :(得分:2)

通过声明自己的xy浮点数,您的Rectangle子类隐藏了Rectangle类的xy字段,因此您永远不会更改它们。 overlap方法继续使用默认的x和y值0.不要在子类中声明xy

答案 1 :(得分:0)

很难说没有看到它运行,但我首先要检查两个潜在的问题......

我要检查的第一件事是你的坐标系。与您绘制的方式相比,请查看您的宽度和高度。

enemy.height = 3;
enemy.width = 6;

bullet.height = 1;
bullet.width = 1;

batch.draw(enemyImage,enemy.x,enemy.y);
batch.draw(bulletImage, bullet.x, bullet.y);

我不确定您的实际图像有多大(以像素为单位) - 但很可能它们与您的宽度和高度不匹配。因此,虽然图像可能重叠,但矩形实际上可能不重叠。没有任何其他上下文,我认为widthheight像素大小 - 所以它们现在非常小。

通过在图像顶部绘制矩形或将图像绘制为矩形的宽度/高度来检查这一点很容易,但我自己知道找到一个是非常具有挑战性的 - 我屏幕上的像素子弹。

我要检查的第二件事,如果你的矩形似乎相互穿过,试着放慢速度,看它是否还会发生。

你可能遇到了一个问题,即子弹和敌人是传送的#34;通过彼此。

例如:I每次更新向下移动2个点,O每次更新向右移动2个点

X X X I X -> X X X X X -> X X X X X
X X X X X    X X X X X    X X X X X
X X X X X    X X X I X    X X X X X
O X X X X    X X O X X    X X X X O //No Collision! "teleported" past each other
X X X X X -> X X X X X -> X X X I X

解决这个问题的常用方法是在你所在的位置和你将要去的地方之间创建一条虚线,并查看它是否与另一个对象的任何虚线相交。