为什么不SpriteBatch画任何东西(LibGdx)? [英] Why doesn't SpriteBatch draw anything (LibGdx)?
问题描述
这是从笨鸟先飞娱乐教程的第6天
- http://www.kilobolt.com/day-6-adding-graphics---welcome-to-the-necropolis.html
This is from day 6 of the flappy bird recreation tutorial -http://www.kilobolt.com/day-6-adding-graphics---welcome-to-the-necropolis.html
下面是图像文件我使用纹理在我的游戏。这是一个256PX点¯x64PX PNG文件。
Here is the image file i am using for texture in my game. It is a 256px x 64px .png file.
下面是我用来加载纹理和具体TextureRegion(该texure的一部分)类,我想SpriteBatch绘制。
Here is the class that I used for loading the texture and the specific TextureRegion(part of the texure) that I want the SpriteBatch to draw.
public class AssetLoader {
public static Texture texture;
public static TextureRegion bg;
public static void load() {
texture = new Texture(Gdx.files.internal("data/texture.png"));
bg = new TextureRegion(texture, 0, 0, 136, 43);
}
}
And I call AssertLoader.load(), along with setting up game screen from
public class MyGdxGame extends Game{
@Override
public void create() {
AssetLoader.load();
setScreen(new GameScreen());
}
}
And inside GameScreen.java
public class GameScreen implements Screen {
//delegate render task
private GameRenderer renderer;
public GameScreen() {
float screenHeight = Gdx.graphics.getHeight();
float screenWidth = Gdx.graphics.getWidth();
float gameWidth = 136;
float gameHeight = screenHeight / (screenWidth / gameWidth);
renderer = new GameRenderer((int)gameHeight);
}
}
And inside GameRederer, the class I delegate to render the game
public class GameRenderer {
private int gameHeight;
private SpriteBatch batch;
private OrthographicCamera cam;
public GameRenderer(int gameHeight) {
this.gameHeight = gameHeight;
cam = new OrthographicCamera();
batch = new SpriteBatch();
batch.setProjectionMatrix(cam.combined);
cam.setToOrtho(true, 136, gameHeight);
}
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.disableBlending();
batch.draw(AssetLoader.bg, 0, (gameHeight/2) + 23, 136, 43);
batch.end()
}
}
我所得到的,当我运行游戏的桌面版以上(黑色显示,因为我背景设置为黑色,code
What I get when I run the desktop version of the game is the black screen shown above(black because i set the background to black with these lines of code
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
有谁知道为什么SpritchBatch图纸没有显示出来?我摘录了我想用这条线code(0,0,从开始的136宽度,43的高度)的质感纹理部分 - 用于GIMP - 找出部分进行切割。
Does anyone know why the SpritchBatch drawing isn't showing up? I extracted the texture portion of the texture I wanted with this line of code(starts from 0,0, width of 136, height of 43) - used GIMP - to find out portion to cut.
bg = new TextureRegion(texture, 0, 0, 136, 43);
我也放了几个日志报表(从视图中删除它们),以确保图纸,BG的宽度和高度分别设置正确,这是他们之前。而问题不能游戏的高度,因为我用了一个打印语句,发现是204,这意味着这个前pression,(gameHeight / 2)+ 23将评估125这是在边界0和游戏之间高度。
I also put a few log statements(removed them from view) to ensure that before drawing, bg's width and height were set correctly, which they were. And the issue can't be game height because I used a print statement and found that to be 204 which means that this expression, (gameHeight/2) + 23 will evaluate to 125 which is in bounds between 0 and game height.
我检查了其他线程为好。
我的问题不能 libgdx spritebatch不渲染纹理的,因为SpriteBatch应该覆盖的背景。
而且它不能 LibGDX:Android的SpriteBatch不拉丝因为我对矿井运行桌面,不Andorid的。
I checked out other threads as well. My issue can't be libgdx spritebatch not rendering textures because the SpriteBatch should overwrite the background. And it can't be LibGDX: Android SpriteBatch not drawing because i am running mine on desktop, not andorid.
推荐答案
可能是,你必须首先把 cam.setToOrtho(真实的,136,gameHeight);
前批,所以我不能肯定希望能帮助
could be that you have to first put cam.setToOrtho(true, 136, gameHeight);
before the batch, so I can not confirm hopefully help
public GameRenderer(int gameHeight) {
this.gameHeight = gameHeight;
cam = new OrthographicCamera();
batch = new SpriteBatch();
cam.setToOrtho(true, 136, gameHeight);
batch.setProjectionMatrix(cam.combined);
}
这篇关于为什么不SpriteBatch画任何东西(LibGdx)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!