Zahidul
Zahidul

Reputation: 399

cocos2d-android: how to display score

I added CCLabel in my update method to display my game score.
It works well before score raise to 5000. After that logCat shows the messege:

02-08 11:47:37.476: E/dalvikvm-heap(4190): 1048576-byte external allocation too large for this process.
02-08 11:47:37.476: E/dalvikvm(4190): Out of memory: Heap Size=14343KB, Allocated=13585KB, Bitmap Size=2078KB
java.lang.reflect.InvocationTargetException......
caused by java.lang.OutOfMemoryError

My code is:

countScore++ ;
Log.e("total Score:", "" + countScore);
    CCLabel labelScore = CCLabel.makeLabel("" + countScore, "DroidSans", 20);

    labelScore.setColor(new ccColor3B(1,1,1));
    labelScore.setPosition(CGPoint.ccp(50, 50));
    addChild(labelScore, 11);
    labelScore.setTag(11);
    _labelScores.add(labelScore);
    CCCallFuncN actionMoveDone1 = CCCallFuncN.action(this, "labelFinished");
    CCSequence action = CCSequence.actions(actionMoveDone1);
    labelScore.runAction(action);

How to fix it?

Upvotes: 1

Views: 2180

Answers (2)

Mihir Palkhiwala
Mihir Palkhiwala

Reputation: 2584

I think you are creating CCLabel every time when you need.

CCLabel labelScore = CCLabel.makeLabel("" + countScore, "DroidSans", 20);
labelScore.setColor(new ccColor3B(1,1,1));
labelScore.setPosition(CGPoint.ccp(50, 50));
addChild(labelScore, 11);
labelScore.setTag(11);

Don't do that.
Set your ScoreLable as global variable and complete its initialization, color setting and positioning in constructor. In your condition use only following code.

labelScore.setString("" + countScore);

Upvotes: 3

badgerr
badgerr

Reputation: 7982

Unless labelFinished does some cleanup that we can't see (you haven't shown us that code), It looks like you are creating 5000 labels.

You should store a single CCLabel as a class member and use setString instead of creating a new label for every score increment.

Better yet, you should use a CCLabelAtlas instead of CCLabel for frequently changing labels (such as scores).

Upvotes: 1

Related Questions