2015-10-16 145 views
0

我目前正在使用libGDX並遇到了一個奇怪的問題。 我用批處理和矩形的紋理被拉伸。這是一個picture。 正如你所看到的,背景看起來完全正常,但中間的人比左邊角的人高很多,應該看起來像這樣。 這裏是我的代碼:libGDX紋理「拉伸」

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,那麼左側角落的人看起來是一樣的,但中間的人比較小。仍然無法弄清楚如何解決它雖然...

回答

0

問題是,您的levelStage使用不同的相機。

new Stage(); 

使用自己的相機和縮放視口創建舞臺。在這裏你創建另一個相機:

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

請注意,這不會設置窗口大小!

使用靜態大小,因爲在調整窗口大小時從不更新它。

由於您使用的舞臺,你的水平,你可以這樣做:

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

如果你不想要一個縮放視窗創建自己的(去看一下一個不同的視口),並將其添加爲參數爲new Stage(viewport)

要設置窗口大小,您需要轉到桌面項目並在配置類中進行更改。

+0

謝謝你,工作就像一個魅力<3。 – Joshflux