2013-03-19 156 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.