fix JSB crash of label when texture not loaded

This commit is contained in:
Jare Guo
2017-04-18 17:19:24 +08:00
parent 900d7236cf
commit ecb242f046
2 changed files with 34 additions and 23 deletions
+2 -1
View File
@@ -353,7 +353,8 @@ if (CC_DEV) {
"4008": "cc.LabelBMFont.initWithString(): Impossible to create font. Please check file", //
"4009": "cocos2d: LabelBMFont: character not found %s", //BMFont.createFontChars
"4010": "cc.LabelBMFont.setFntFile() : Impossible to create font. Please check file", //BMFont.setFntFile
"4011": "Property spriteFrame of BitmapFont '%s' is invalid. Using system font instead.", // BMFont spriteFrame is invalid.
"4011": "Property spriteFrame of Font '%s' is invalid. Using system font instead.", // BMFont spriteFrame is invalid.
"4012": "The texture of Font '%s' must be already loaded on JSB. Using system font instead.",
//Layout: 4100
"4100": "Property padding is deprecated, please use paddingLeft, paddingRight, paddingTop and paddingBottom instead", //padding
//Mask: 4200
+32 -22
View File
@@ -386,20 +386,26 @@ var Label = cc.Class({
cc.warnID(4000);
}
var isAsset = value instanceof cc.Font;
if (this.font instanceof cc.BitmapFont) {
if (this.font.spriteFrame) {
var font = this.font;
if (font instanceof cc.BitmapFont) {
if (font.spriteFrame) {
if (!CC_JSB) {
this._sgNode.setFontFileOrFamily(this.font.fntDataStr, this.font.spriteFrame, this.font);
this._sgNode.setFontFileOrFamily(font.fntDataStr, font.spriteFrame, font);
} else {
this._sgNode.setFontFileOrFamily(this.font.fntDataStr, this.font.spriteFrame);
if (font.spriteFrame.textureLoaded()) {
this._sgNode.setFontFileOrFamily(font.fntDataStr, font.spriteFrame);
}
else {
cc.warnID(4012, font.name);
this._sgNode.setFontFileOrFamily('');
}
}
} else {
cc.warnID(4011, this.font.name);
cc.warnID(4011, font.name);
this._sgNode.setFontFileOrFamily('');
}
} else {
var isAsset = value instanceof cc.Font;
var ttfName = isAsset ? value.rawUrl : '';
this._sgNode.setFontFileOrFamily(ttfName);
}
@@ -498,27 +504,31 @@ var Label = cc.Class({
},
_initSgNode: function () {
if ( typeof this.font === 'string' ) {
var font = this.font;
if (typeof font === 'string' ) {
cc.warnID(4000);
}
var isAsset = this.font instanceof cc.Font;
var sgNode;
if (this.font instanceof cc.BitmapFont) {
if (this.font.spriteFrame) {
if (font instanceof cc.BitmapFont) {
if (font.spriteFrame) {
if (CC_JSB) {
sgNode = this._sgNode = new _ccsg.Label(this.string, this.font.fntDataStr, this.font.spriteFrame);
if (font.spriteFrame.textureLoaded()) {
sgNode = this._sgNode = new _ccsg.Label(this.string, font.fntDataStr, font.spriteFrame);
} else {
cc.warnID(4012, font.name);
sgNode = this._sgNode = new _ccsg.Label(this.string);
}
} else {
sgNode = this._sgNode = new _ccsg.Label(this.string, this.font.fntDataStr, this.font.spriteFrame, this.font);
sgNode = this._sgNode = new _ccsg.Label(this.string, font.fntDataStr, font.spriteFrame, font);
}
} else {
cc.warnID(4011, this.font.name);
cc.warnID(4011, font.name);
sgNode = this._sgNode = new _ccsg.Label(this.string);
}
} else {
var ttfName = isAsset ? this.font.rawUrl : '';
var isAsset = font instanceof cc.Font;
var ttfName = isAsset ? font.rawUrl : '';
sgNode = this._sgNode = new _ccsg.Label(this.string, ttfName);
}
@@ -526,8 +536,8 @@ var Label = cc.Class({
sgNode.retain();
}
if (this.font instanceof cc.BitmapFont) {
this._bmFontOriginalSize = this.font.fontSize;
if (font instanceof cc.BitmapFont) {
this._bmFontOriginalSize = font.fontSize;
}
sgNode.setVisible(false);
@@ -538,16 +548,16 @@ var Label = cc.Class({
sgNode.enableWrapText( this._enableWrapText );
sgNode.setLineHeight(this._lineHeight);
sgNode.setString(this.string);
if (this.font instanceof cc.BitmapFont) {
if (font instanceof cc.BitmapFont) {
sgNode.setSpacingX(this.spacingX);
}
if (CC_EDITOR) {
this._userDefinedFontSize = this.fontSize;
this._userDefinedFont = this.font;
this._userDefinedFont = font;
}
if (CC_EDITOR && this._useOriginalSize) {
this.node.setContentSize(sgNode.getContentSize());
if (this.font instanceof cc.BitmapFont) {
if (font instanceof cc.BitmapFont) {
this.lineHeight = sgNode.getBMFontLineHeight();
}
this._useOriginalSize = false;