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

时间:2016-01-25 15:51:09

标签: libgdx scene2d

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

2 个答案:

答案 0 :(得分:3)

查看Label类,特别是采用CharSequence和LabelStyle的构造函数。初始化LabelStyle时,您可以提供BitmapFont。

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

答案 1 :(得分:0)

你可以扩展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));