简体   繁体   English

libGDX纹理被“拉伸”

[英]libGDX textures are “stretched”

I am currently working with libGDX and got to a strange problem. 我目前正在使用libGDX,遇到一个奇怪的问题。 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 干杯,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. 如果我将窗口的高度从800调整为200,则左角的人看起来相同,但中间的人要小得多。 Still can't figure out how to solve it though... 虽然还是不知道如何解决...

The problem is that your levelStage uses a different camera. 问题是您的levelStage使用其他相机。

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) . 如果您不希望缩放视口,请创建自己的视口(看看其他视口),并将其作为参数添加到new Stage(viewport)

To set the window size you need to go to the desktop project and change it in the config class. 要设置窗口大小,您需要转到桌面项目并在config类中进行更改。

That happens because you are adding an actor to a scene and just drawing an texture after. 发生这种情况是因为您要在场景中添加一个actor,然后才绘制纹理。 you could use some information about differences of actors and texture drawing here: 您可以在此处使用有关角色和纹理绘制差异的一些信息:

libgdx difference between sprite and actor Sprite和Actor之间的libgdx区别

When to use actors in libgdx? 何时在libgdx中使用actor? What are cons and pros? 什么是利弊?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM