我對libgdx和android編程一般都很陌生。我在渲染基於sprite-sheet的動畫時遇到問題,並且在不同的屏幕尺寸上獲得相同的尺寸。libgdx基於sprite-sheet的動畫縮放
如果我在筆記4上運行以下代碼,動畫非常小,在Zenfone 2上它相當大,最後在我的筆記本電腦上,它很小,幾乎看不到它。
我真的不明白爲什麼會發生這種情況,以及如何使它在兩部手機上相同。我認爲使用帶有遊戲單元和視口的正交相機可以完成這項工作,但是我可能會做錯事,因爲事實並非如此。
我正在關注「libgdx跨平臺遊戲開發菜譜」一書。
我非常感謝任何關於如何正確使用遊戲單位以獲得遊戲在不同屏幕尺寸上相同的幫助,以便在Note4上顯示512x512像素的圖像並且在Zenfone上顯示巨大的圖像(我的動畫每幀都是512px平方)。
就pc而言,我只是不知道發生了什麼,我真的很感激任何解釋爲什麼會發生!
謝謝大家!
package com.mygdxGame;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import java.util.Comparator;
public class MyGdxGame extends ApplicationAdapter {
private static final float WORLD_TO_SCREEN = 1.0f/100.0f;
private static final float SCENE_WIDTH = 12.80f;
private static final float SCENE_HEIGHT = 7.20f;
private static final float FRAME_DURATION = 1.0f/20.0f;
private TextureAtlas techmanAtlas;
private Animation techmanRun;
private float animationTime;
private OrthographicCamera camera;
public Viewport viewport;
public SpriteBatch batch;
@Override
public void create(){
camera = new OrthographicCamera();
viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
batch = new SpriteBatch();
animationTime = 0.0f;
techmanAtlas = new TextureAtlas(Gdx.files.internal("TechMan.atlas"));
Array<TextureAtlas.AtlasRegion> techmanRegions = new Array<TextureAtlas.AtlasRegion>(techmanAtlas.getRegions());
techmanRegions.sort(new RegionComparator());
techmanRun = new Animation(FRAME_DURATION, techmanRegions, PlayMode.LOOP);
camera.position.set(SCENE_WIDTH * 0.5f, SCENE_HEIGHT * 0.5f, 0.0f);
}
@Override
public void dispose(){
batch.dispose();
techmanAtlas.dispose();
}
@Override
public void render(){
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
animationTime += Gdx.graphics.getDeltaTime();
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
TextureRegion techmanFrame = techmanRun.getKeyFrame(animationTime);
int width = techmanFrame.getRegionWidth();
int height = techmanFrame.getRegionWidth();
float originX = width * 0.5f;
float originY = height * 0.5f;
batch.draw(techmanFrame,
1.0f - originX, 3.70f - originY,
originX, originY,
width, height, //width, height
WORLD_TO_SCREEN, WORLD_TO_SCREEN,
0.0f);
batch.draw(techmanRun.getKeyFrame(animationTime), 100.0f, 275.0f);
batch.end();
}
@Override
public void resize(int width, int height){
viewport.update(width, height, false);
}
private static class RegionComparator implements Comparator<AtlasRegion> {
@Override
public int compare(AtlasRegion region1, AtlasRegion region2){
return region1.name.compareTo(region2.name);
}
}
}
謝謝!那麼會在視口中傳遞「SCENE_WIDTH」和「SCENE_HEIGHT」嗎?在batch.draw中,第一個參數應該是「techman.Frame」,然後是寬度高度x y? –
@EdoardoPona是的,它會工作。你應該選擇世界座標,使你的世界中容易計算的東西(我建議你製作概念藝術的相同分辨率)。是的,我修復了繪製方法中的錯誤,謝謝 –
是的!有效!我不得不添加最後一件事,在Gdx.gl.clclear方法中:「| GL20.GL_DEPTH_BUFFER_BIT」,謝謝! –