Skip to content

Commit fe5cec5

Browse files
committed
Merge pull request #1 from VisualSJ/develop-cocosudio
Issue #5096: Optimization code about speed
2 parents ee89ea7 + 6329c24 commit fe5cec5

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

cocos2d/core/labelttf/CCLabelTTF.js

+37-9
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,16 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
823823
return this._labelContext;
824824
},
825825

826+
_maxNum: function(numArr){
827+
var num = numArr[0];
828+
for(var i=1;i<numArr.length;i++){
829+
if(num<numArr[i]){
830+
num = numArr[i];
831+
}
832+
}
833+
return num;
834+
},
835+
826836
_updateTTF: function () {
827837
var locDimensionsWidth = this._dimensions.width, i, strLength;
828838
var locLineWidth = this._lineWidths;
@@ -842,7 +852,11 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
842852
i += next;
843853
}
844854
} else {
845-
this._strings = this._string.split('\n');
855+
if(/\n/.test(this._string)) {
856+
this._strings = this._string.split('\n');
857+
}else{
858+
this._strings = [this._string];
859+
}
846860
for (i = 0, strLength = this._strings.length; i < strLength; i++) {
847861
locLineWidth.push(this._measure(this._strings[i]));
848862
}
@@ -863,19 +877,33 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
863877
//get offset for stroke and shadow
864878
if (locDimensionsWidth === 0) {
865879
if (this._isMultiLine)
866-
locSize = cc.size(0 | (Math.max.apply(Math, locLineWidth) + locStrokeShadowOffsetX),
867-
0 | ((this._fontClientHeight * this._strings.length) + locStrokeShadowOffsetY));
880+
locSize = {
881+
width: this._maxNum(locLineWidth) + locStrokeShadowOffsetX | 0,
882+
height: this._fontClientHeight * this._strings.length + locStrokeShadowOffsetY | 0
883+
};
868884
else
869-
locSize = cc.size(0 | (this._measure(this._string) + locStrokeShadowOffsetX), 0 | (this._fontClientHeight + locStrokeShadowOffsetY));
885+
locSize = {
886+
width: this._measure(this._string) + locStrokeShadowOffsetX | 0,
887+
height: this._fontClientHeight + locStrokeShadowOffsetY | 0
888+
};
870889
} else {
871890
if (this._dimensions.height === 0) {
872891
if (this._isMultiLine)
873-
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | ((this._fontClientHeight * this._strings.length) + locStrokeShadowOffsetY));
892+
locSize = {
893+
width: locDimensionsWidth + locStrokeShadowOffsetX | 0,
894+
height: this._fontClientHeight * this._strings.length + locStrokeShadowOffsetY | 0
895+
};
874896
else
875-
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | (this._fontClientHeight + locStrokeShadowOffsetY));
897+
locSize = {
898+
width: locDimensionsWidth + locStrokeShadowOffsetX | 0,
899+
height: this._fontClientHeight + locStrokeShadowOffsetY | 0
900+
};
876901
} else {
877902
//dimension is already set, contentSize must be same as dimension
878-
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | (this._dimensions.height + locStrokeShadowOffsetY));
903+
locSize = {
904+
width: locDimensionsWidth + locStrokeShadowOffsetX | 0,
905+
height: this._dimensions.height + locStrokeShadowOffsetY | 0
906+
};
879907
}
880908
}
881909
this.setContentSize(locSize);
@@ -884,8 +912,8 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
884912

885913
// need computing _anchorPointInPoints
886914
var locAP = this._anchorPoint;
887-
this._anchorPointInPoints.x = (locStrokeShadowOffsetX * 0.5) + ((locSize.width - locStrokeShadowOffsetX) * locAP.x);
888-
this._anchorPointInPoints.y = (locStrokeShadowOffsetY * 0.5) + ((locSize.height - locStrokeShadowOffsetY) * locAP.y);
915+
this._anchorPointInPoints.x = (0.5 - locAP.x) * locStrokeShadowOffsetX + locAP.x * locSize.width;
916+
this._anchorPointInPoints.y = (0.5 - locAP.y) * locStrokeShadowOffsetY + locAP.y * locSize.height;
889917
},
890918

891919
getContentSize: function () {

0 commit comments

Comments
 (0)