我创建了一个帮助类,它将根据我给出的自定义字体和文本返回Text对象.
这是代码:
public class CustomFontTextHelper {
private Font font;
private ITexture fontTexture;
private Text text;
public Text getTextWithFont(String myText,String fontPath,BaseGameActivity activity,float x,float y,int fontsize,int color){
fontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
FontFactory.setAssetBasePath("fonts/");
this.font = FontFactory.createFromAsset(activity.getFontManager(), fontTexture, activity.getAssets(), fontPath, fontsize, true, color);
this.font.load();
text = new Text(x, y, this.font, myText, activity.getVertexBufferObjectManager());
return text;
}
}
Run Code Online (Sandbox Code Playgroud)
使用这个帮助器类我创建一个文本并附加到我的场景.它的工作完美.但是当我尝试使用text.setText方法更改文本时,它会崩溃.
下面是我用来更改文本的代码.
public class StartingScreen extends BaseGameActivity {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 720;
// ===========================================================
// Fields
// ===========================================================
public Text loadingPercentage;
public float percentLoaded;
public CustomFontTextHelper fontHelper;
@Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
@Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0, 0, 0));
fontHelper = new CustomFontTextHelper();
percentLoaded = 0;
loadingPercentage = fontHelper.getTextWithFont("0%", "MyriadPro-Cond.ttf", this, CAMERA_WIDTH-120, 100, 64, Color.BLACK);
scene.attachChild(loadingPercentage);
pOnCreateSceneCallback.onCreateSceneFinished(scene);
}
@Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
pOnPopulateSceneCallback.onPopulateSceneFinished();
loadingPercentage.setText("5%");
new Thread(new Runnable(){
@Override
public void run() {
int incr;
for (incr = 0; incr <= 100; incr+=5) {
StartingScreen.this.runOnUpdateThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
StartingScreen.this.loadingPercentage.setText(Float.toString(StartingScreen.this.percentLoaded) + "%");
}
});
try {
Thread.sleep(5*1000);
} catch (InterruptedException e) {
}
}
// TODO Auto-generated method stub
}
}).start();
// TODO Auto-generated method stub
}
Run Code Online (Sandbox Code Playgroud)
请帮我解释一下代码.
据我所知,你正在使用AndEngine GLES2,因为Matim已经说过你可以改变Text实例.问题是,当你Text第一次需要包含一些文本或更好的文本来实例化对象时,你应该告诉对象它必须保持多少个字母(最大).而不是
text = new Text(x, y, this.font, myText, activity.getVertexBufferObjectManager());
Run Code Online (Sandbox Code Playgroud)
你做
int textLength = 4;
text = new Text(x, y, this.font, myText, textLength, activity.getVertexBufferObjectManager());
Run Code Online (Sandbox Code Playgroud)
我只是将长度设置为4,因为你将它用作百分比,只能在0%到100%之间,所以最多4个字母.当然,您可以根据需要设置文本长度,如果将其设置为1000并且只在文本中放置"hello",那么它很好(如果可以的话,可以将它们设置为小以节省内存).
你之后无法重置文本大小 - 这意味着,一旦你创建了一个Text object长度为5的你不能在其中放入6个字母,你也不能调用像setSize(6)那样的东西......那是行不通的.
我希望这有帮助!
| 归档时间: |
|
| 查看次数: |
2608 次 |
| 最近记录: |