cocos2d-android: how to display score

谁说胖子不能爱 提交于 2019-12-23 01:43:51

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/9188591/cocos2d-android-how-to-display-score

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!