libGDX纹理被拉伸"

时间:2015-10-16 18:10:46

标签: java libgdx

我目前正在使用libGDX并遇到一个奇怪的问题。 我用于批处理和矩形的纹理被拉伸。这是一个picture。 正如你所看到的,背景看起来完全正常,但是中间的人比左角的人高很多,它应该看起来像。 这是我的代码:

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();
}}

我希望你能帮我找到错误。 干杯, Joshflux

编辑:我很确定,它必须对窗口的大小做一些事情。如果我将窗口的高度从800调整到200,左角的人看起来是一样的,但中间的人要小一些。仍然无法弄清楚如何解决它......

2 个答案:

答案 0 :(得分:0)

问题是你的levelStage使用不同的相机。

new Stage();

使用自己的相机和缩放视口创建舞台。在这里你创建了另一个摄像头:

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

请注意,这不会设置窗口大小!

使用静态大小,因为在调整窗口大小时永远不会更新它。

由于您为级别使用舞台,因此可以执行此操作:

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

如果您不希望缩放视口创建自己的(查看不同的视口)并将其作为参数添加到new Stage(viewport)

要设置窗口大小,您需要转到桌面项目并在配置类中更改它。

答案 1 :(得分:0)

之所以发生这种情况,是因为您正在向场景中添加一个actor,之后只是绘制一个纹理。你可以在这里使用一些有关演员和纹理绘图差异的信息:

libgdx difference between sprite and actor

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