Cocos2dx-2.2.1 LabelTTF setString causes huge lag!

Hello everyone.
I created simple game with collision detection. I have one LabelTTF with default font for displaying total score. After executing following code:

label.setString("Score: "+totalScore);

fps decrease to around 30 fps. When I don’t execute this line, fps is over 58.
Tested on Samsung Galaxy Note 2 and Samsung Galaxy S2.

So updating label’s string causes huge lag. Where is the problem?

same with mine the fps drop though unnoticeable sometimes frustrates me. - Rabbi Binyomin Lisbon

Are you calling that method a lot? The LabelTTF has to construct the texture each time. You should not create a LabelTTF frequently. If you need to change a label frequently you should use the LableBitmapFont.

Yep, I used it to show total score, so it was updated many times in every second.

Is LabelBitmapFont texture scalable? Or fixed size?

Because:

The main difference between CCLabelTTF and CCLabelAtlas is that the atlas version (like all the other atlas classes)
uses one big texture with all the letters pre-rendered to draw a string.
This means that the drawing is much faster, because if you draw 100 labels,
the graphics processor doesn't have to read in 100 textures but just keep one texture in memory.
But it also means that all the letters will be of a fixed size.
If you want to get around the fixed-size limitation, use CCBitmapFontAtlas.

via: http://www.cocos2d-x.org/wiki/Text_Labels#CCLabelTTF-vs-CCLabelAtlas

You can scale a CCLabelBitmapFont, but it would be blurry if scaled too much. Instead you should just generate a bitmap font that is large enough for your game.

Have you tried not updating it every frame? I seriously doubt your score will change that fast (or that the player will even notice).

I usually go with something like this:

//somewhere in your gamelayer's init:
...
this.schedule(this.updateLabels, 1/10);
...

updateLabels:function(){
    lblScore.setString("Score: "+totalScore);
}

And that’s only because the score is tied to the elapsed time (in milliseconds) in my game, if that wasn’t the case it would work just fine with an even lower refresh rate.

PS: also, I might be wrong, but I think they improved LabelTTF’s performance in v2.2.2, so maybe you could try upgrading?

I updated my LabelTTF when gamer collected an item. Item can be collected not faster than every 0.4 second. Every collect event caused lag, when I executed method lblScore.setString. It wasn’t matter how often I did it.
But I will try updating it with scheduler. Thanks :slight_smile: