Search code examples
androidcocos2d-iphonecocos2d-android

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?


Solution

  • 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);