From d8dbbda902f3564712fdcf7c1bbca3fdab167fc2 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 4 Feb 2016 00:47:47 +0800 Subject: [PATCH] Fix more JSB crash issues --- cocos2d/core/CCScene.js | 4 --- cocos2d/core/components/CCComponentInSG.js | 3 +++ cocos2d/core/components/CCLabel.js | 6 +++-- cocos2d/core/utils/base-node.js | 3 +-- jsb/jsb-label.js | 30 ++++++++++++++++++++-- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/cocos2d/core/CCScene.js b/cocos2d/core/CCScene.js index ac698faf21..4de1ed6580 100644 --- a/cocos2d/core/CCScene.js +++ b/cocos2d/core/CCScene.js @@ -63,10 +63,6 @@ cc.Scene = cc.Class({ this._activeInHierarchy = false; }, - _onPreDestroy: function () { - this._sgNode.release(); - }, - _onHierarchyChanged: NIL, _onColorChanged: NIL, _onAnchorChanged: NIL, diff --git a/cocos2d/core/components/CCComponentInSG.js b/cocos2d/core/components/CCComponentInSG.js index 6c6e85723c..8841d323d7 100644 --- a/cocos2d/core/components/CCComponentInSG.js +++ b/cocos2d/core/components/CCComponentInSG.js @@ -29,6 +29,9 @@ var ComponentInSG = cc.Class({ * @private */ this._sgNode = this._createSgNode(); + // retain immediately + // will be released in SceneGraphHelper.removeSgNode + this._sgNode.retain(); }, onLoad: function () { diff --git a/cocos2d/core/components/CCLabel.js b/cocos2d/core/components/CCLabel.js index c22267b8fe..acb9cb91ed 100644 --- a/cocos2d/core/components/CCLabel.js +++ b/cocos2d/core/components/CCLabel.js @@ -230,8 +230,10 @@ var Label = cc.Class({ this.node._sizeProvider = null; } - // node should be resize whenever font changed - this._sgNode.on('load', this._updateNodeSize, this); + // node should be resize whenever font changed, needed only on web + if (!cc.sys.Native) { + this._sgNode.on('load', this._updateNodeSize, this); + } }, onDestroy: function () { diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index d77978f308..8cf76ec3ef 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -642,9 +642,8 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, - destroy: function () { + _onPreDestroy: function () { this._sgNode.release(); - this._super(); }, // ABSTRACT INTERFACES diff --git a/jsb/jsb-label.js b/jsb/jsb-label.js index 23adf9f716..37e8b9a994 100644 --- a/jsb/jsb-label.js +++ b/jsb/jsb-label.js @@ -79,7 +79,7 @@ jsbLabel.prototype.getTTFConfig = function () { return this._ttfConfig; }; -jsbLabel.prototype.setContentSize = function(size, height) { +jsbLabel.prototype.setContentSize = function (size, height) { if(height !== undefined){ this.setDimensions(size, height); } @@ -87,6 +87,32 @@ jsbLabel.prototype.setContentSize = function(size, height) { this.setDimensions(size.width, size.height); } }; + +jsbLabel.prototype.setFontFileOrFamily = function (fontHandle) { + fontHandle = fontHandle || ''; + var extName = cc.path.extname(fontHandle); + + //specify font family name directly + if (extName === null) { + this._labelType = _ccsg.Label.Type.SystemFont; + this.setSystemFontName(fontHandle); + } + else { + //add resource path + fontHandle = cc.path.join(cc.loader.resPath, fontHandle); + + if (extName === '.ttf') { + this._labelType = _ccsg.Label.Type.TTF; + this._ttfConfig.fontFilePath = fontHandle; + this.setTTFConfig(this._ttfConfig); + } else if (extName === '.fnt') { + //todo add bmfont here + this._labelType = _ccsg.Label.Type.BMFont; + this.setBMFontFilePath(fontHandle, cc.v2(0, 0), this.getBMFontSize()); + } + } +}; + cc.Label = function (string, fontHandle) { fontHandle = fontHandle || "Arial"; var extName = cc.path.extname(fontHandle); @@ -111,7 +137,7 @@ cc.Label = function (string, fontHandle) { type = _ccsg.Label.Type.BMFont; } else { - label = jsbLabel.createWithSystemFont(string, fontHandle, 40); + label = jsbLabel.createWithSystemFont(string || '', fontHandle, 40); type = _ccsg.Label.Type.SystemFont; } label._labelType = type;