【发布时间】:2016-05-02 00:05:42
【问题描述】:
我正在使用 libgdx 框架开发游戏。如何在位图字体对象上实现 scene2d 动作?这样我就可以写一些文本,比如得分、消息和像场景 2d 演员一样运行动作。
【问题讨论】:
我正在使用 libgdx 框架开发游戏。如何在位图字体对象上实现 scene2d 动作?这样我就可以写一些文本,比如得分、消息和像场景 2d 演员一样运行动作。
【问题讨论】:
你可以扩展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));
【讨论】:
查看Label 类,特别是采用 CharSequence 和 LabelStyle 的构造函数。初始化 LabelStyle 时,您可以提供 BitmapFont。
请注意,如果您想缩放或旋转标签,您需要将其包裹在 Container 中,或者在启用 setTransform() 的情况下将其添加到 Table。 (这会刷新 SpriteBatch,因此请明智地使用它。)
【讨论】: