我正在用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() { }}
解决方法:
从libGDX的version 1.5.6开始,方法BitmapFont.computeGlyphAdvancesAndpositions was removed改为GlyphLayout
.
BitmapFont.computeGlyphAdvancesAndpositions返回了2个数组,其中包含给定CharSequence的字符的进阶和位置.
要使用GlyphLayout检索相同的信息,请执行以下 *** 作:
>创建一个GlyphLayout对象并设置所需的字体和文本:
GlyphLayout layout = new GlyphLayout();layout.setText(Font, text);
>提供的文本将基于换行符拆分为“运行”;如果没有换行符,则整个文本将在第一次运行时存储:
GlyphRun run = layout.runs.get(0);
> GlyphRun
包含xAdvances,它与BitmapFont.computeGlyphAdvancesAndpositions返回的advances数组相当;区别在于,数组的第一个位置包含相对于绘图位置的X偏移量,而第i个字符的实际前进宽度位于数组的第i个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),如先前所示(或以等效方式)获得
以上是内存溢出为你收集整理的java-Libgdx的computeGlyphAdvancesAndPositions不存在全部内容,希望文章能够帮你解决java-Libgdx的computeGlyphAdvancesAndPositions不存在所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)