1

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?

Mihir Palkhiwala
  • 2,586
  • 3
  • 37
  • 47
Zahidul
  • 389
  • 5
  • 15

2 Answers2

3

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);
Mihir Palkhiwala
  • 2,586
  • 3
  • 37
  • 47
1

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).

badgerr
  • 7,802
  • 2
  • 28
  • 43
  • public void labelFinished(Object sender){ CCLabel label = (CCLabel) sender; if(label.getTag()== 11) _labelScores.remove(label); this.removeChild(label, true); } this is my labelFinised method @ badgerr – Zahidul Feb 08 '12 at 11:15
  • Fair enough. Did you try CCLabelAtlas? – badgerr Feb 08 '12 at 11:22
  • http://stackoverflow.com/questions/17037813/how-to-use-cclistview-in-cocos2d-android here is my question – ishu Jun 13 '13 at 11:16