我正在开发一个使用libgdx框架的游戏。如何在位图字体对象上实现scene2d操作?这样我就可以像scene2d actor一样编写一些文本,如得分、消息和运行操作。
发布于 2016-01-26 08:07:58
看看Label类,特别是接受CharSequence和LabelStyle参数的构造函数。在初始化LabelStyle时,您可以提供一个BitmapFont。
请注意,如果您想缩放或旋转标签,则需要将其包装在Container中,或者在启用setTransform()的情况下将其添加到Table中。(这会刷新SpriteBatch,所以要明智地使用它。)
发布于 2016-01-26 00:04:31
您可以扩展actor类来实现相同的功能。
例如:-
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFontCache;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.scenes.scene2d.Actor;
public class FontActor extends Actor
{
private Matrix4 matrix = new Matrix4();
private BitmapFontCache bitmapFontCache;
private GlyphLayout glplayout;
public FontActor(float posX, float posY, String fontText)
{
BitmapFont fnt=new BitmapFont(Gdx.files.internal("time_newexport.fnt"),
Gdx.files.internal("time_ne-export.png"),false);
bitmapFontCache = new BitmapFontCache(fnt);
glplayout=bitmapFontCache.setText(fontText, 0, 0);
setPosition(posX, posY);
setOrigin(glplayout.width / 2, -glplayout.height/2);
}
@Override
public void draw(Batch batch, float alpha)
{
Color color = getColor();
bitmapFontCache.setColor(color.r, color.g, color.b, color.a*alpha);
matrix.idt();
matrix.translate(getX(), getY(), 0);
matrix.rotate(0, 0, 1, getRotation());
matrix.scale(getScaleX(), getScaleY(), 1);
matrix.translate(-getOriginX(), -getOriginY(), 0);
batch.setTransformMatrix(matrix);
bitmapFontCache.draw(batch);
}
public void setAlpha(int a)
{
Color color = getColor();
setColor(color.r, color.g, color.b, a);
}
public void setText(String newFontText)
{
glplayout = bitmapFontCache.setText(newFontText, 0, 0);
setOrigin(glplayout.width / 2, -glplayout.height/2);
}
}你可以像这样使用它。
Actor actor=new FontActor(20,30,"test");
stage.addActor(actor);
actor.addAction(Actions.moveTo(10,10,1));https://stackoverflow.com/questions/34996693
复制相似问题