2013-03-19 111 views
0

我創建了一個幫助器類,它將根據我給出的自定義字體和文本返回Text對象。AndEngine Text.setText not working

這裏是代碼:

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 

    } 

請幫我編寫代碼。

回答

6

至於我使用的是AndEngine GLES2,從而Matim已經說了,你可以改變Text實例看。問題在於,當您第一次實例化Text對象時,您需要包含一些文本或更好的文本,您應該告訴該對象需要保存多少個字母(最大值)。 所以不是

text = new Text(x, y, this.font, myText, activity.getVertexBufferObjectManager()); 

你做

int textLength = 4; 
text = new Text(x, y, this.font, myText, textLength, activity.getVertexBufferObjectManager()); 

我剛纔設置的長度4,因爲你把它作爲一個百分比,這隻能是間0%100%所以最多4個字母。當然你可以根據需要設置文本長度,如果你設置爲1000,並且只在文本中加上「hello」,那麼它很好(如果可以的話,使它們變小以節省內存)。

之後你就不能重置文本大小 - 意思是說,一旦你創建了一個長度爲5的Text object,你就不能在其中放置6個字母,也不能調用像setSize(6)這樣的東西。 。

我希望這有助於!

  • 克里斯托夫
+0

謝謝你。那就是訣竅 – 2013-03-19 14:21:12

0

文本只設置你的文本值,如果你想改變你的文本,然後使用ChangeableText你不能改變的文本,

ChangeableText changeableText = new ChangeableText(x, y, Font, "Your Text"); 
yourscene.attachChild(changeableText); 
changeableText.setText("Your New Text"); 
+1

這一切取決於他正在使用的引擎的哪個分支,如果他使用的是GLES1(舊的,過時的,他不應該使用它)在GLES2版本(和更新版本)中不再有ChangeableText,而是我們只有Text,並且在運行時更新文本。 – Matim 2013-03-19 13:10:42

1

正如遊戲機器人指出,這個問題是與文本的長度。不過,我想同時回答這個問題和下一個問題。接下來的問題是,我該如何避免改變文本從一個值到另一個時的延遲,而解決方案是將文本與所有計劃使用,像這樣的人物初始化:

Text text = new Text(x, y, this.font, "1234567890%",activity.getVertexBufferObjectManager()); 
text.setText("0%"); 

這構造函數的形式將最大文本長度設置爲初始字符串長度的任何值,因此在這種情況下爲11.