1
\$\begingroup\$

I'm facing a little problem and I have no idea to bypass it. I've made a dungeon generator and I'm able to spawn randomly mobs, the problem is that when they are not moving the game is fine, but at the moment they move, all other sprite (+ fps drawing on the top left) disappear. This problem was not existant when I was using the version < 1.0 and I'm now on the 1.2 (the last). I'm using a customized Sprite class but it is working until I enter an enemy aggro circle.

What I'm doing when my game is updating is : Update player (mouvement, ...) Updating spell (reach the end, hit someone, ...) Update enemy (does the player is inside the circle of his aggro ? If so, is he behind a wall or I can see him ? If I can see him, I move toward him)

The problem is on the enemy update, when the enemy see me, everything disappear.

Here is a jar of my game to see the problem in action (commands are Z Q S D to move). Move a bit toward the guard and you'll see.

Download link

Here it's ok : enter image description here

Here I'm getting the aggro : enter image description here

Here is the class used and maybe causing the bug :

(update method of the main class) :

        @Override
    public void render() {
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT | GL30.GL_DEPTH_BUFFER_BIT);
        renderer.setView(camera);
        mouvementCamera(camera.zoom);
        camera.update();   
        renderer.render();
        batch.setProjectionMatrix(camera.combined);
        player.update();
        batch.begin();
        for(int i = 0; i < player.getSpell().size(); i++) {
            Spell s = player.getSpell().get(i);
                if(s.getalive()) {
                    s.update(this);
                    batch.draw(s.getTextureRegion(s.currentAnim), s.getX(), s.getY());
                }
                else {
                    player.removeElements(s);
                    i--;
                }
        }
        for(Enemy e : enemy) {
            if(e.isAlive()) {
                e.update();
                batch.draw(e.getTextureRegion(e.current_anim), e.getX(), e.getY());
            }
            else {
                enemy.remove(e);
                break;
            }
        }
        batch.draw(player.getTextureRegion(player.current_anim), player.getX(), player.getY());
        fps.draw(batch, String.valueOf(Gdx.graphics.getFramesPerSecond()), camera.position.x + 500, camera.position.y + 200);
        batch.end();
    }

(player update method)

@Override
public void update() {
    float delta = Gdx.graphics.getDeltaTime();
    for(Cooldown cd : this.tabCd) {
        cd.update(delta);
    }

    float oldY = this.y;
    float oldX = this.x;

    if(Gdx.input.isKeyPressed(Keys.Z)) {
        if(y+collisionHeight < this.map.mapHeight * this.map.tileHeight) 
            y += this.stats.speed;
        this.current_anim = TOP;
    }
    if(Gdx.input.isKeyPressed(Keys.S)) {
        if(y >= 0)
            y -= this.stats.speed;
        this.current_anim = BOT;
    }
    if(Gdx.input.isKeyPressed(Keys.Q)) {
        if(x >= 0)
            x -= this.stats.speed;
        this.current_anim = LEFT;
    }
    if(Gdx.input.isKeyPressed(Keys.D)) {
        if(x+collisionWidth < this.map.mapWidth * this.map.tileWidth)
            x += this.stats.speed;
        this.current_anim = RIGHT;
    }
    if(Gdx.input.isKeyPressed(Keys.R) && tabCd.get(0).getendCast()) {
        // directionx et directionY sont les coordonnees de la souris
        int directionX = recupDirectionX();
        int directionY = recupDirectionY();
        int centreX = calcCentre(this.x, this.collisionWidth, this.diffCollisionWidth);
        int centreY = calcCentre(this.y, this.collisionHeight, this.diffCollisionHeight);
        double norme = calcNorme(directionY, centreY, directionX, centreX);
        double newY = (directionY - centreY) / norme;
        double newX = (directionX - centreX) / norme;
        spell.add(new FireBall(x, y, 10, 10, 800, 5, newX, newY));
        tabCd.get(0).setendCast(false);
        tabCd.get(0).resetCast();
    }
    if(Gdx.input.isKeyPressed(Keys.T) && tabCd.get(1).getendCast()) {
        int directionX = recupDirectionX();
        int directionY = recupDirectionY();
        int centreX = calcCentre(this.x, this.collisionWidth, this.diffCollisionWidth);
        int centreY = calcCentre(this.y, this.collisionHeight, this.diffCollisionHeight);
        double norme = calcNorme(directionY, centreY, directionX, centreX);
        double newY = (directionY - centreY) / norme;
        double newX = (directionX - centreX) / norme;
        spell.add(new FireBall(x, y, 10, 10, 200, 5, newX, newY));
        tabCd.get(1).setendCast(false);
        tabCd.get(1).resetCast();
    }
    if(Gdx.input.isButtonPressed(Buttons.LEFT) && tabCd.get(2).getendCast()) {
        spell.add(new Attack((x + ((collisionWidth / 2) - diffCollisionWidth) - (this.stats.sizeWeapon / 2)),
                y + (collisionHeight / 2), this.stats.rangeAttack, this.stats.sizeWeapon));
        tabCd.get(2).setendCast(false);
        tabCd.get(2).resetCast();
    }

    if(gestion_collision(x, oldY)) {
        this.x = oldX;
    }
    if(gestion_collision(oldX, y)) {
        this.y = oldY;
    }
}

(enemy update method)

    @Override
public void update() {
    float playerX = this.map.player.x;
    float playerY = this.map.player.y;
    int collisionWidth = this.map.player.collisionWidth;
    int collisionHeight = this.map.player.collisionHeight;
    this.tempX = 0;
    this.tempY = 0;
    Circle circle = new Circle(this.x + (this.collisionWidth / 2),
            this.y + (this.collisionHeight / 2),
            (float)this.rayonDetection);
    if(circle.contains(playerX + (collisionWidth / 2), playerY + (collisionHeight / 2))) {
        int xPlayer = (int)playerX + (collisionWidth / 2) + this.map.player.diffCollisionWidth;
        int yPlayer = (int)playerY + (collisionHeight / 2) + this.map.player.diffCollisionHeight ;
        int xEnnemy = (int)this.x + (this.collisionWidth / 2) + diffCollisionWidth;
        int yEnnemy = (int)this.y + (this.collisionHeight / 2) + diffCollisionHeight;
        double norme = Math.sqrt((Math.pow((yPlayer - yEnnemy), 2) + Math.pow((xPlayer - xEnnemy), 2)));
        double newX = (xPlayer - xEnnemy) / norme;
        double newY = (yPlayer - yEnnemy) / norme;
        int result = checkNormeInWall(norme, newX, newY);
        if(result == 1 || result == 2) {
            if(result == 1)
                detect = true;
            if(!detect)
                return;
            getWay();
        }
        else {
                this.tempX = (float) newX;
                this.tempY = (float) newY;          
                this.detect = true;
        }   

        if(!collision(x, ((tempY * this.stats.speed) + y)) && !collisionInEntity(
                x + this.diffCollisionWidth,
                ((tempY * this.stats.speed) + y) + this.diffCollisionHeight, 
                this.map.player.x + this.map.player.diffCollisionWidth, 
                this.map.player.y + this.map.player.diffCollisionHeight,
                this.map.player.collisionHeight, 
                this.map.player.collisionWidth)) {
            setY((tempY * this.stats.speed) + y);
        }
        if(!collision(((tempX * this.stats.speed) + x), y) && !collisionInEntity(
                ((tempX * this.stats.speed) + x) + this.diffCollisionWidth,
                y + this.diffCollisionHeight,
                this.map.player.x + this.map.player.diffCollisionWidth,
                this.map.player.y + this.map.player.diffCollisionHeight,
                this.map.player.collisionHeight,
                this.map.player.collisionWidth)) {
            setX((tempX * this.stats.speed) + x);
        }
    }
    else {
        this.detect = false;
    }
    setAnim(tempX, tempY);
}

private void getWay() {
    chemin = Pathfinding.generatePath(this.map.mapWidth, this.map.mapHeight, 
            this.map, (int)this.x / this.map.tileWidth, (int)this.y / this.map.tileHeight);
    int xEnnemy = (int)this.x;
    int yEnnemy = (int)this.y;
    double norme = Math.sqrt( (Math.pow((yEnnemy - (chemin.y*this.map.tileHeight)), 2) 
            + Math.pow((xEnnemy - (chemin.x * this.map.tileWidth)), 2)));
    this.tempX = (float)(((chemin.x * this.map.tileWidth) - xEnnemy) / norme);
    this.tempY = (float)(((chemin.y * this.map.tileHeight) - yEnnemy) / norme);     
}

Thanks !

\$\endgroup\$
1
  • 2
    \$\begingroup\$ "Help me debug" questions are usually off-topic, and you've posted reams of code. Can you try narrowing it down? Binary search is good (comment out half the code, if it works, recursively comment/uncomment half the code) to quickly get to the offending part. \$\endgroup\$
    – ashes999
    Jun 25 '14 at 14:19
0
\$\begingroup\$

I pretty much find the source of the problem. When i was launching the game, the memory of the game was litterly growing every second (+10K Ko / s). I isolated the problem and the source of it is this "part" of code :

protected boolean collisionInEntity(float x, float y, float xObject, float yObject, int collisionHeight, int collisionWidth) {
    Rectangle player = new Rectangle(x, y, this.collisionWidth, this.collisionHeight);
    ShapeRenderer shapeRenderer = new ShapeRenderer();
    shapeRenderer.setProjectionMatrix(map.camera.combined);
    shapeRenderer.begin(ShapeType.Line);
    shapeRenderer.setColor(new Color(0, 1, 0, 1));
    shapeRenderer.rect(x, y, this.collisionWidth, this.collisionHeight);
    shapeRenderer.end();
    Rectangle ennemy = new Rectangle(xObject, yObject, collisionWidth, collisionHeight);
    if(player.overlaps(ennemy)) {
        return true;
    }
    return false;
}

Have you any idea of what could be the reason of this memory leak ?

\$\endgroup\$
2
  • \$\begingroup\$ Just a guess, are you sure you should be creating a new ShapeRenderer instance every single collision check? \$\endgroup\$
    – ashes999
    Jun 25 '14 at 18:57
  • \$\begingroup\$ I tried to create it only on a monster / player creation i have no more memory problem. But i still have my sprite disappearing ! If i comment the shapeRenderer part, my sprites are working fine. I think there is definitly a problem with this ShapeRenderer class. \$\endgroup\$
    – Kamigaku
    Jun 26 '14 at 7:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.