我查看了其他Stackoverflow问题并完成了答案告诉我的内容,但我似乎无法让我的班级显示任何文字。
这是我的代码,其中显示了所有文本:
package com.frinkly.jumpcat;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.OrthographicCamera;
public class WorldRenderer {
private static final float CAMERA_WIDTH = 12f;
private static final float CAMERA_HEIGHT = 20f;
private OrthographicCamera cam;
private SpriteBatch spriteBatch;
private int width;
private int height;
private float ppuX;
private float ppuY;
private BitmapFont font;
private World world;
private Cat cat;
ShapeRenderer debugRenderer = new ShapeRenderer();
public WorldRenderer(World newWorld) {
this.world = newWorld;
this.cat = world.getCat();
this.cam = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
this.cam.position.set(CAMERA_WIDTH / 2f, CAMERA_HEIGHT / 2f, 0);
spriteBatch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("font/font.fnt"), false);
font.setColor(Color.BLACK);
this.cam.update();
spriteBatch.setProjectionMatrix(new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
}
public void setSize(int newWidth, int newHeight) {
width = newWidth;
height = newHeight;
ppuX = (float) width / CAMERA_WIDTH;
ppuY = (float) height / CAMERA_HEIGHT;
}
public void render() {
cam.update();
spriteBatch.begin();
debugDraw();
drawPoints();
spriteBatch.end();
}
private void drawPoints() {
font.setColor(1.0f, 1.0f, 1.0f, 1.0f);
font.setScale(1, height/width);
font.draw(spriteBatch, Integer.toString(cat.getPoints()), 0.5f, height/width*0.5f);
}
private void debugDraw() {
debugRenderer.setProjectionMatrix(cam.combined);
debugRenderer.begin(ShapeType.Filled);
for(Block block : world.getBlocks()) {
Rectangle rect1 = block.getBounds1();
Rectangle rect2 = block.getBounds2();
float x1 = block.getPosition().x;//+block.getPosition().x
float y1 = block.getPosition().y;//+block.getPosition().y
debugRenderer.setColor(new Color(1, 0, 0, 1));
debugRenderer.rect(x1, y1, rect1.width, rect1.height);
debugRenderer.rect(x1, y1 + rect1.height + block.getSPACE(), rect2.width, rect2.height);
}
Rectangle rect = cat.getBounds();
float x1 = cat.getPosition().x + rect.x;
float y1 = cat.getPosition().y + rect.y;
debugRenderer.setColor(new Color(0, 1, 0, 1));
debugRenderer.rect(x1, y1, rect.width, rect.height);
debugRenderer.end();
}
private void drawBlocks() {
}
private void drawCat() {
}
}
这是调用渲染器显示文本的GameScreen:
package com.frinkly.jumpcat;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.Screen;
public class GameScreen implements Screen, InputProcessor{
private World world;
private WorldRenderer renderer;
private CatController catController;
private BlockController blockController;
private int screenWidth;
private int screenHeight;
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
blockController.update(delta);
catController.update(delta);
renderer.render();
}
@Override
public void resize(int width, int height) {
screenWidth = width;
screenHeight = height;
renderer.setSize(width, height);
}
@Override
public void show() {
loadAssets();
world = new World();
renderer = new WorldRenderer(world);
catController = new CatController(world);
blockController = new BlockController(world);
blockController.start();
Gdx.input.setInputProcessor(this);
}
private void loadAssets() {
}
@Override
public void hide() {
Gdx.input.setInputProcessor(null);
}
@Override
public void pause() {
Gdx.input.setInputProcessor(null);
}
@Override
public void resume() {
Gdx.input.setInputProcessor(this);
}
@Override
public void dispose() {
}
@Override
public boolean keyDown(int keycode) {
if(keycode == Keys.SPACE) {
catController.jumpPressed();
}
return true;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if(!Gdx.app.getType().equals(ApplicationType.Android)) {
return false;
}
catController.jumpPressed();
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if(!Gdx.app.getType().equals(ApplicationType.Android)) {
return false;
}
return true;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return false;
}
}
我的字体位于正确的文件夹中,我使用Hiero生成文件。我使用了150个大小,它生成了font1png-font5.png,我也把它放在我的字体文件夹中。
答案 0 :(得分:3)
错误应该在render()
方法中:
cam.update();
spriteBatch.begin();
debugDraw();
drawPoints();
spriteBatch.end();
在debugDraw()
内begin()
ShapeRenderer
。所以2个对象试图同时绘制。首先debugDraw()
然后begin()
执行SpriteBatch
之后:
cam.update();
debugDraw();
spriteBatch.begin();
drawPoints();
spriteBatch.end();
希望有所帮助