Libgdx 如何使用位图字体作为场景2d演员?
我正在使用libgdx框架开发一个游戏。如何在位图字体对象上实现scene2d操作?这样我就可以编写一些文本,比如score、message和运行动作,比如scene2dactor。您可以扩展actor类来实现同样的功能 比如:-Libgdx 如何使用位图字体作为场景2d演员?,libgdx,scene2d,Libgdx,Scene2d,我正在使用libgdx框架开发一个游戏。如何在位图字体对象上实现scene2d操作?这样我就可以编写一些文本,比如score、message和运行动作,比如scene2dactor。您可以扩展actor类来实现同样的功能 比如:- import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.BitmapFontCac
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));
看看这个类,特别是采用CharSequence和。初始化LabelStyle时,可以提供位图字体
请注意,如果要缩放或旋转标签,则需要将其包装在一个文件夹中,或将其添加到启用了setTransform()的文件夹中。(这会刷新SpriteBatch,因此请明智地使用它。)