我创建了一个帮助类,它将根据我提供的自定义字体和文本返回 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;
}
}
使用这个助手类,我创建了一个文本并附加到我的场景中。它工作完美。但是当我尝试使用 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
}
请帮我写代码。