Geo Paul
Geo Paul

Reputation: 1817

AndEngine Text.setText not working

I created a helper class that will return Text object based on the custom font and text I give.

Here is code :

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;
    }
}

Using this helper class I create a text and attach to my scene. Its working perfectly. But when I try to change the text using text.setText method it crashes.

Below is the code I use to change the text.

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

    }

Please help me with the code.

Upvotes: 0

Views: 2636

Answers (3)

OneThreeSeven
OneThreeSeven

Reputation: 422

As pointed out by game droid, the problem is with the text length. However I would like to answer this question and the next one at the same time. The next question is, how do I avoid the delay when changing the text from one value to another, and the solution is to initialize the text with all of the characters you plan to use, like so:

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

This form of the constructor sets the maximum text length to whatever the length of the initial string is, so in this case 11.

Upvotes: 1

GameDroids
GameDroids

Reputation: 5662

As far as I can see you are using AndEngine GLES2, so as Matim already said you can change the Text instance. The problem is that when you instantiate the Text object the first time you either need to include some text or better you should tell the object how many letters it has to hold (max). so instead of

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

you do

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

I just set the length to 4 since you use it as a percentage, which can only be between 0% and 100% so maximal 4 letters. Of course you can set the text length as you want, if you set it to 1000 and only put "hello" in your text, its fine (if you can, make them small to save memory) .

You just can't reset the text size afterwards – meaning, once you created a Text object with length 5 you can't put 6 letters in it, nor can you call something like setSize(6).. that's not working.

I hope this helps!

  • Christoph

Upvotes: 6

Divyang Metaliya
Divyang Metaliya

Reputation: 1918

Text is only set your text value, you can't change that text,

if you want change your text then use ChangeableText.

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

Upvotes: 0

Related Questions