Nassif Bousaba
Nassif Bousaba

Reputation: 396

libGdx getWorldHeight returning wrong value

I'm creating a game using libGdx and I'm facing a difficulty with getWorldHeight() parameter. This is my code:

public Level(Viewport Viewport)
{
    this.viewport = Viewport;
    player = new Player(viewport,this);

    pbullets = new DelayedRemovalArray<PlayerBullet>();
    powerUps = new DelayedRemovalArray<PowerUps>();
    MediumAsteroid = new DelayedRemovalArray<Asteroid>();
    enemySpaceships = new DelayedRemovalArray<EnemySpaceShip>();

    startTime_POWERUP = TimeUtils.millis();
    startTime_ASTEROID = TimeUtils.millis();


    float Xpos = (viewport.getWorldWidth()/2);
    float Ypos = viewport.getWorldHeight();

    Gdx.app.log("YPOS ", Float.toString(Ypos));
    enemySpaceships.add(new EnemySpaceShip(new Vector2(Xpos,Ypos),this));


}

The world size is 128x128. The Ypos, which is set to viewport.getWorldHeight() is returning 0. However, in the same class, I call this method again during the update callback, and it returns 128. EXAMPLE:

public void SpawnPowerUp()
{
    float Xpos = MathUtils.random(Constants.POWERUP_WIDTH,viewport.getWorldWidth()) - Constants.POWERUP_WIDTH;

    float Ypos = viewport.getWorldHeight();

    Vector2 position = new Vector2(Xpos,Ypos);
    powerUps.add(new PowerUps(position,this));
}

For my knowledge, the viewport.getWorldHeight() should return the full world Height which is 128. Why is it returning 0 in this case?

Upvotes: 1

Views: 132

Answers (1)

Madmenyo
Madmenyo

Reputation: 8584

Might be because in the constructor, create and show method the vp is not fully initialized yet. Did you try vp.apply()? I might test it today if I can find the time.

Upvotes: 1

Related Questions