【发布时间】:2014-02-15 08:35:02
【问题描述】:
我非常努力地用 LibGDX 绘制精灵,但我失败了。如果您需要的不仅仅是 sn-ps,来源在这里: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);
}
我正在测试大小为 800x480 的 Winows 版本。 我要绘制的精灵的原点是:X 300.0 Y 139.0 宽度为 300,高度为 140。 接下来是渲染函数:
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 的各种解决方案
最后是“create”方法:
// 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);
}
感谢您的帮助
弗洛里安
【问题讨论】: