我正在用Libgdx做文字动画。尝试做类似的事情

font1 = new BitmapFont(Gdx.files.internal("fontLabel/fonty.fnt"), false);
font1.getRegion().getTexture()
        .setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
touchPos = new Vector3();

listchar = new TextButton[pause.length()];
advances = new FloatArray();
post = new FloatArray();
font1.computeGlyphAdvancesAndPositions(pause, advances, post);

但是computeGlyphAdvancesAndPositions不再退出,该怎么办?

编辑

我读了这个blog post,上面说要使用GlyphLayout,但是我不知道该怎么做? GlyphLayout不接受我要提供的参数

我想做类似this video中的动画的操作,旧的源代码是here,但是如上所述,由于我强调了这一节,所以它不再起作用。
package com.tntstudio.texteffect;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.FloatArray;

public class TECore implements ApplicationListener {
    private Stage stage;
    private BitmapFont font;
    private TextButton[] listchar;
    private FloatArray post, advances;
    private String text;
    private static final float time = 0.5f, delay = 0.2f;
    private static final float x = 200f, y = 200f;

    @Override
    public void create() {
        stage = new Stage(800f, 480f, true);
        font = new BitmapFont(Gdx.files.internal("data/texteffect.fnt"));
        font.getRegion().getTexture()
                .setFilter(TextureFilter.Linear, TextureFilter.Linear);
        text = "manh phi libgdx";
        listchar = new TextButton[text.length()];
        advances = new FloatArray();
        post = new FloatArray();
        font.computeGlyphAdvancesAndPositions(text, advances, post);

        final TextButtonStyle style = new TextButtonStyle();
        style.font = font;

        /*-------- List Text --------*/
        for (int i = 0; i < text.length(); i++) {
            listchar[i] = new TextButton(String.valueOf(text.charAt(i)), style);
            listchar[i].setTransform(true);
            listchar[i].setPosition(x + post.get(i), y);
            listchar[i].setOrigin(advances.get(i) / 2,
                    listchar[i].getHeight() / 4);
            stage.addActor(listchar[i]);
        }

        Gdx.input.setInputProcessor(stage);

        /*-------- Drop Effect Adapter --------*/
        TextButton drop = new TextButton("drop", style);
        drop.setPosition(0, 10);
        drop.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                dropText();
            }
        });
        stage.addActor(drop);

        /*-------- Spin effect Adapter --------*/
        TextButton spin = new TextButton("spin", style);
        spin.setPosition(0, 100f);
        spin.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                spinText();
            }
        });
        stage.addActor(spin);
        /*-------- Appear effect Adapter --------*/
        TextButton appear = new TextButton("appear", style);
        appear.setPosition(0, 300f);
        appear.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                appearText();
            }
        });
        stage.addActor(appear);

    }

    // ///////////////////////////////////////////////////////////////
    // Reset Param of a char in text
    // ///////////////////////////////////////////////////////////////
    private void resetText() {
        for (int i = 0; i < text.length(); i++) {
            listchar[i].setPosition(x + post.get(i), y);
            listchar[i].setOrigin(advances.get(i) / 2,
                    listchar[i].getHeight() / 4);
            listchar[i].setColor(0, 0, 0, 1);
            listchar[i].setScale(1f);
        }
    }

    private void dropText() {
        resetText();
        for (int i = 0; i < text.length(); i++) {
            listchar[i].setY(y + 200f);
            listchar[i].setColor(0, 0, 0, 0);
            listchar[i].addAction(Actions.delay(
                    delay * i,
                    Actions.parallel(Actions.alpha(1, time), Actions.moveTo(x
                            + post.get(i), y, time, Interpolation.bounceOut))));
        }
    }

    private void spinText() {
        resetText();
        for (int i = 0; i < text.length(); i++) {
            listchar[i].addAction(Actions.delay(delay * i,
                    Actions.rotateBy(360f, time * 5, Interpolation.elastic)));
        }
    }

    private void appearText(){
        resetText();
        for (int i=0; i<text.length(); i++){
            listchar[i].setScale(0f);
            listchar[i].setColor(0, 0, 0, 0);
            listchar[i].addAction(Actions.delay(delay*i, Actions.parallel(Actions.alpha(1, time), Actions.scaleTo(1, 1, time, Interpolation.swingOut))));
        }
    }

    @Override
    public void dispose() {
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0, 0, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

最佳答案

libGDXversion 1.5.6开始,使用was removed的方法BitmapFont.computeGlyphAdvancesAndPositions GlyphLayout
BitmapFont.computeGlyphAdvancesAndPositions返回2个数组,其中包含给定CharSequence的字符的前进和位置。

要使用GlyphLayout检索相同的信息:

  • 创建一个GlyphLayout对象并设置所需的字体和文本:
    GlyphLayout layout = new GlyphLayout();
    layout.setText(font, text);
    
  • 提供的文本将基于换行符拆分为“运行”;如果没有换行符,则整个文本将在第一次运行时存储:
    GlyphRun run = layout.runs.get(0);
    
  • GlyphRun 包含的xAdvancesBitmapFont.computeGlyphAdvancesAndPositions返回的advances数组相当;区别在于,数组的第一个位置包含相对于绘图位置的X偏移量,而i-th字符的实际前进宽度在数组的i-th + 1元素中:
    float startingOffset = run.xAdvances.get(0);
    float ithAdvance = run.xAdvances.get(i + 1);
    
  • 不幸的是,文本中单个字符的位置不再可用,但是我们可以通过添加先前字符的高级字符来获得它们:
    float[] positions = new float[text.length()];
    for (int i = 0; i < positions.length; i++) {
        if (i == 0) {
            // first position is the starting offset
            positions[0] = run.xAdvances.get(0);
        } else {
            // current position is the previous position plus its advance
            positions[i] = positions[i - 1] + run.xAdvances.get(i);
        }
    }
    

  • 结论:
  • advances.get(i)替换为run.xAdvances.get(i + 1)
  • post.get(i)替换为positions[i],如先前所示(或以等效方式)获得
  • 10-08 08:26