From ecb242f0469a6723cf9e3a69506e2dfcb183acb8 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Tue, 18 Apr 2017 17:19:24 +0800 Subject: [PATCH] fix JSB crash of label when texture not loaded --- DebugInfos.js | 3 +- cocos2d/core/components/CCLabel.js | 54 ++++++++++++++++++------------ 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/DebugInfos.js b/DebugInfos.js index de7fb170a2..b24462e8c1 100644 --- a/DebugInfos.js +++ b/DebugInfos.js @@ -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 diff --git a/cocos2d/core/components/CCLabel.js b/cocos2d/core/components/CCLabel.js index 6ca591211d..1b8d25694f 100644 --- a/cocos2d/core/components/CCLabel.js +++ b/cocos2d/core/components/CCLabel.js @@ -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;