应绘制字符串 - 没有错误,使用矢量
string should be drawn - no erros, using vector
我希望在条件语句中出现游戏结束画面,下面的代码不会产生任何错误或白色字母。它应该采用白色文本并将其显示在黑色背景上,但它不起作用。我什至使用 Vector3 确保坐标正确。
package com.mygdx.game;
import java.util.Random;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Input;
//import com.mygdx.game.IsFinished;
import com.badlogic.gdx.Screen;
import java.util.Random;
import java.util.Timer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.mygdx.game.*;
class GameOverScreen implements Screen{
Label gameoverstring;
private Stage stage;;
SpriteBatch spritebatch;
int placex=0, placey=0;
@Override
public void show() {
stage = new Stage();
BitmapFont white = new BitmapFont(Gdx.files.internal("new.fnt"), false);
LabelStyle headingStyle = new LabelStyle(white, Color.WHITE);
gameoverstring = new Label("game ovaaaa!", headingStyle);
gameoverstring.setFontScale(3);
stage.addActor(gameoverstring);
}
@Override
public void render(float delta) {
Vector3 placeholder = new Vector3(placex, placey, 0);
show();
//gameoverstring.setPosition(0, 0);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameoverstring.setPosition(placeholder.x, placeholder.y);
stage.act(delta);
stage.draw();
System.out.println(gameoverstring.getX()+" "+gameoverstring.getY());
}
清理代码后,它可以正常使用我的字体。
相关代码:
private Stage stage;
// Called automatically once for init objects
@Override
public void show() {
stage = new Stage();
stage.setDebugAll(true); // Set outlines for Stage elements for easy debug
BitmapFont white = new BitmapFont(Gdx.files.internal("new.fnt"), false);
LabelStyle headingStyle = new LabelStyle(white, Color.WHITE);
Label gameoverstring = new Label("game ovaaaa!", headingStyle);
stage.addActor(gameoverstring);
}
// Called every frame so try to put no object creation in it
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
stage.act(delta);
}
注意标签应该在左下角可见。
因此,如果您仍然看不到文字,那么我猜您的字体有问题。也许你的 *.png 中的字体是黑色的,那么我认为 libGdx 无法将字符着色为白色并且它们保持黑色。猜猜 libGdx 通过乘以这些值来给它们着色,并且 0*x
总是 0
。要测试它,您可以将 Gdx.gl.glClearColor(0, 0, 0, 1);
更改为绿色或其他内容。
您还可以查看 setDebugAll(true)
方法中的绿色边界线是否可见。
我希望在条件语句中出现游戏结束画面,下面的代码不会产生任何错误或白色字母。它应该采用白色文本并将其显示在黑色背景上,但它不起作用。我什至使用 Vector3 确保坐标正确。
package com.mygdx.game;
import java.util.Random;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Input;
//import com.mygdx.game.IsFinished;
import com.badlogic.gdx.Screen;
import java.util.Random;
import java.util.Timer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.mygdx.game.*;
class GameOverScreen implements Screen{
Label gameoverstring;
private Stage stage;;
SpriteBatch spritebatch;
int placex=0, placey=0;
@Override
public void show() {
stage = new Stage();
BitmapFont white = new BitmapFont(Gdx.files.internal("new.fnt"), false);
LabelStyle headingStyle = new LabelStyle(white, Color.WHITE);
gameoverstring = new Label("game ovaaaa!", headingStyle);
gameoverstring.setFontScale(3);
stage.addActor(gameoverstring);
}
@Override
public void render(float delta) {
Vector3 placeholder = new Vector3(placex, placey, 0);
show();
//gameoverstring.setPosition(0, 0);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameoverstring.setPosition(placeholder.x, placeholder.y);
stage.act(delta);
stage.draw();
System.out.println(gameoverstring.getX()+" "+gameoverstring.getY());
}
清理代码后,它可以正常使用我的字体。
相关代码:
private Stage stage;
// Called automatically once for init objects
@Override
public void show() {
stage = new Stage();
stage.setDebugAll(true); // Set outlines for Stage elements for easy debug
BitmapFont white = new BitmapFont(Gdx.files.internal("new.fnt"), false);
LabelStyle headingStyle = new LabelStyle(white, Color.WHITE);
Label gameoverstring = new Label("game ovaaaa!", headingStyle);
stage.addActor(gameoverstring);
}
// Called every frame so try to put no object creation in it
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
stage.act(delta);
}
注意标签应该在左下角可见。
因此,如果您仍然看不到文字,那么我猜您的字体有问题。也许你的 *.png 中的字体是黑色的,那么我认为 libGdx 无法将字符着色为白色并且它们保持黑色。猜猜 libGdx 通过乘以这些值来给它们着色,并且 0*x
总是 0
。要测试它,您可以将 Gdx.gl.glClearColor(0, 0, 0, 1);
更改为绿色或其他内容。
您还可以查看 setDebugAll(true)
方法中的绿色边界线是否可见。