0

I am currently working with libGDX and got to a strange problem. The textures that I use with batch and rectangles are stretched. Here is a picture. As you can see, the background looks completely normal, but the person in the middle is a lot taller than the person in the left corner, which it should look like. Here is my code:

import java.util.Iterator;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.Array;
import com.data.Manager;

public class GameScreen implements Screen{

private Texture backgroundTexture = Manager.manager.get(("Ressources/Hintergrund_Skizze.png"), Texture.class);
private Texture farmerBackTexture = Manager.manager.get(("Ressources/Farmer_Back_Skizze.png"), Texture.class);
private Texture farmerRightTexture = Manager.manager.get(("Ressources/Farmer_Right_Skizze.png"), Texture.class);
private Texture farmerLeftTexture = Manager.manager.get(("Ressources/Farmer_Left_Skizze.png"), Texture.class);
private Texture ufoTexture = Manager.manager.get(("Ressources/Ufo_Skizze.png"), Texture.class);
private Texture laserTexture = Manager.manager.get(("Ressources/Magic_Ball.png"), Texture.class);

private Image backgroundImage = new Image(backgroundTexture);
private Image farmerBackImage = new Image(farmerBackTexture);

private Stage levelStage = new Stage(), menuStage = new Stage();

private Table menuTable = new Table();

private Skin menuSkin = Manager.menuSkin;

private OrthographicCamera camera;

private SpriteBatch batch;

private Rectangle farmer, ufo;
private Array<Rectangle> lasers;

private boolean ufoMovementLeft = true;
private boolean leftArrow = false;
private boolean rightArrow = false;

private float laserMovement = 0;
private int ufoLife = 15;

@Override
public void show() {

    levelStage.addActor(backgroundImage);
    levelStage.addActor(farmerBackImage);
    Gdx.input.setInputProcessor(levelStage);
    camera = new OrthographicCamera();
    camera.setToOrtho(false, 800, 480);
    batch = new SpriteBatch();

    farmer = new Rectangle();
    farmer.x = 800 / 2 - 80 / 2; farmer.y = 80;
    farmer.width = 80; farmer.height = 270;

    ufo = new Rectangle();
    ufo.x = 800 / 2; ufo.y = 375;
    ufo.width = 185; ufo.height = 94;

    lasers = new Array<Rectangle>();
}

public void movement() {

    if(Gdx.input.isKeyPressed(Keys.LEFT)) leftArrow = true;
    if(!Gdx.input.isKeyPressed(Keys.LEFT)) leftArrow = false;
    if(Gdx.input.isKeyPressed(Keys.RIGHT)) rightArrow = true;
    if(!Gdx.input.isKeyPressed(Keys.RIGHT)) rightArrow = false;

}

@Override
public void render(float delta) {

     Gdx.gl.glClearColor(0, 0, 0, 1);
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
     camera.update();
     levelStage.act();
     levelStage.draw();

     batch.setProjectionMatrix(camera.combined);
     batch.begin();
     movement();
     if(leftArrow) {if(farmer.x >= 60) farmer.x -= 2; batch.draw(farmerLeftTexture, farmer.x, farmer.y);}
     else if(rightArrow) {if(farmer.x <= 590) farmer.x += 2; batch.draw(farmerRightTexture, farmer.x, farmer.y);}
     else batch.draw(farmerBackTexture, farmer.x, farmer.y);

     batch.end();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void hide() {
}

@Override
public void dispose() {
    levelStage.dispose(); menuStage.dispose();
    menuSkin.dispose();
    ufoTexture.dispose();
    laserTexture.dispose();
    farmerBackTexture.dispose();
    farmerRightTexture.dispose();
    farmerLeftTexture.dispose();
}}

I hope you are able to help me find the mistake. Cheers, Joshflux

EDIT: I am pretty sure, that it has to do something with the size of the window. If I resize the height of the window from 800 to 200, the person in the left corner looks the same, but the person in the middle is way smaller. Still can't figure out how to solve it though...

Joshflux
  • 131
  • 1
  • 10

2 Answers2

0

The problem is that your levelStage uses a different camera.

new Stage();

Creates a stage with its own camera and a scaling viewport. And here you create another camera:

camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);

Note that this does NOT set the window size!

With a static size, as you never update it when you resize the window.

Since you use a stage for your level you could do this:

batch.setProjectionMatrix(levelStage.getCamera().combined);

If you don't want a scaling viewport create your own (take a look a the different viewports) and add it as an parameter to new Stage(viewport).

To set the window size you need to go to the desktop project and change it in the config class.

Pinkie Swirl
  • 2,375
  • 1
  • 20
  • 25
0

That happens because you are adding an actor to a scene and just drawing an texture after. you could use some information about differences of actors and texture drawing here:

libgdx difference between sprite and actor

When to use actors in libgdx? What are cons and pros?

Community
  • 1
  • 1
Hllink
  • 918
  • 5
  • 17