首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Bitmap字体作为场景2d演员?

如何使用Bitmap字体作为场景2d演员?
EN

Stack Overflow用户
提问于 2016-01-25 23:51:09
回答 2查看 623关注 0票数 0

我正在开发一个使用libgdx框架的游戏。如何在位图字体对象上实现scene2d操作?这样我就可以像scene2d actor一样编写一些文本,如得分、消息和运行操作。

EN

回答 2

Stack Overflow用户

发布于 2016-01-26 08:07:58

看看Label类,特别是接受CharSequence和LabelStyle参数的构造函数。在初始化LabelStyle时,您可以提供一个BitmapFont。

请注意,如果您想缩放或旋转标签,则需要将其包装在Container中,或者在启用setTransform()的情况下将其添加到Table中。(这会刷新SpriteBatch,所以要明智地使用它。)

票数 3
EN

Stack Overflow用户

发布于 2016-01-26 00:04:31

您可以扩展actor类来实现相同的功能。

例如:-

代码语言:javascript
复制
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);

     }

}

你可以像这样使用它。

代码语言:javascript
复制
 Actor actor=new FontActor(20,30,"test");
 stage.addActor(actor);
 actor.addAction(Actions.moveTo(10,10,1));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34996693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档