[英]Libgdx fail to draw sprite
我非常努力地用LibGDX繪制精靈,但是失敗了。 如果您需要的不僅僅是片段,請在此處查找源: https : //github.com/reisi007/Planes2DLibGDX/blob/master/Main/src/com/reisisoft/Planes2D/Planes2D.java
那么,我在做什么? 我正在加載PNG(1024x1024)並創建精靈,並將其放置在地圖中。 我再次放入地圖(分辨率不同)
private void putTextures(Resolutions resolutions) {
Map<GObjects, Sprite> map;
switch (resolutions) {
case MidRes:
map = new EnumMap<GObjects, Sprite>(GObjects.class);
map.put(GObjects.PlaneR, new Sprite(txtMidRes, 0, 0, 600, 278));
map.put(GObjects.PlaneG, new Sprite(txtMidRes, 0, 278, 600, 278));
map.put(GObjects.PlaneB, new Sprite(txtMidRes, 0, 556, 600, 104));
map.put(GObjects.Grass, new Sprite(txtMidRes, 0, 834, 600, 104));
map.put(GObjects.Bullet, new Sprite(txtMidRes, 600, 834, 299, 138));
all.put(Resolutions.MidRes, map);
break;
}
}
我可以從當前的分辨率中得到一個精靈:
private boolean setCurrentResolutions(Resolutions resolution) {
if (resolution == null)
return false;
currentResolution = resolution;
currentResolutionSprites = all.get(Resolutions.MidRes);
return true;
}
private Sprite requestSprite(GObjects gObject) {
return currentResolutionSprites.get(gObject);
}
我正在測試Winows版本,其尺寸為800x480。 我要繪制的精靈的原點是:X 300.0 Y 139.0寬度為300,高度為140。接下來是render函數:
public void render() {
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
Update();
spriteBatch.begin();
Draw();
spriteBatch.end();
}
由於這實際上並沒有幫助,因此這里使用Draw方法:
public void Draw() {
s.draw(spriteBatch);
}
我看到的只是黑色。 我嘗試使用OpenGL 1,1.1,2和StackOverflow的各種解決方案
最后是“創建”方法:
// On creation
public void create() {
curH = Gdx.graphics.getHeight();
curW = Gdx.graphics.getWidth();
camera = new OrthographicCamera(curW, curH);
spriteBatch = new SpriteBatch();
// Load texture
txtMidRes = new Texture(Gdx.files.internal("planes.png"));
txtMidRes.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
// Create Sprites for EVERY supported resolution
putTextures(Resolutions.MidRes);
// Set resolution according to H/W
setCurrentResolutions(Resolutions.MidRes);
s = requestSprite(GObjects.PlaneR);
s.setSize(300, 140);
s.setPosition(Gdx.graphics.getWidth() / 2 - s.getWidth() / 2,
Gdx.graphics.getHeight() / 2 - s.getHeight() / 2);
}
感謝您的幫助
弗洛里安
首先,您提供給完整源的鏈接與那里的代碼段不匹配。 也許有一個被遺忘的推動?
現在有關您的問題的一些想法:
您尚未設置相機位置。 嘗試:
camera.position.set(curH / 2f,curW / 2f,0);
您無需手動構建Sprite,libgdx提供了實現此目標的功能。 請參閱官方文檔上的Texture Packer 。
您也可以使用AssetManager自動處理分辨率。 有關更多信息,請參見此帖子 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.