From 077b6638ebb4e0beb5169580687b91784919df2f Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 27 Jan 2016 16:10:51 +0800 Subject: [PATCH 1/2] make it likely to initialize comp._sgNode just in constructor --- cocos2d/core/CCNode.js | 46 ++++--- cocos2d/core/components/CCComponentInSG.js | 13 +- cocos2d/core/components/CCLabel.js | 76 +++-------- cocos2d/core/components/CCSprite.js | 13 +- cocos2d/particle/CCParticleSystem.js | 143 ++++++++------------- 5 files changed, 108 insertions(+), 183 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 92a1435485..b1ecd2ac63 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -812,7 +812,7 @@ var Node = cc.Class({ // update components if also in scene graph for (var c = 0; c < this._components.length; ++c) { var comp = this._components[c]; - if (comp instanceof cc._ComponentInSG && comp.isValid) { + if (comp instanceof cc._ComponentInSG && comp.isValid && comp._sgNode) { comp._sgNode.setColor(this._color); if ( !this._cascadeOpacityEnabled ) { comp._sgNode.setOpacity(this._opacity); @@ -826,7 +826,7 @@ var Node = cc.Class({ var opacity = this._cascadeOpacityEnabled ? 255 : this._opacity; for (var c = 0; c < this._components.length; ++c) { var comp = this._components[c]; - if (comp instanceof cc._ComponentInSG && comp.isValid) { + if (comp instanceof cc._ComponentInSG && comp.isValid && comp._sgNode) { comp._sgNode.setOpacity(opacity); } } @@ -836,7 +836,7 @@ var Node = cc.Class({ // update components if also in scene graph for (var c = 0; c < this._components.length; ++c) { var comp = this._components[c]; - if (comp instanceof cc._ComponentInSG && comp.isValid) { + if (comp instanceof cc._ComponentInSG && comp.isValid && comp._sgNode) { comp._sgNode.setAnchorPoint(this._anchorPoint); comp._sgNode.ignoreAnchorPointForPosition(this._ignoreAnchorPointForPosition); } @@ -846,7 +846,7 @@ var Node = cc.Class({ _onOpacityModifyRGBChanged: function () { for (var c = 0; c < this._components.length; ++c) { var comp = this._components[c]; - if (comp instanceof cc._ComponentInSG && comp.isValid) { + if (comp instanceof cc._ComponentInSG && comp.isValid && comp._sgNode) { comp._sgNode.setOpacityModifyRGB(this._opacityModifyRGB); } } @@ -1095,23 +1095,27 @@ var Node = cc.Class({ // In JSB, when inner sg node being replaced, the system event listeners will be cleared. // We need a mechanisme to guarentee the persistence of system event listeners. if (cc.sys.isNative) { - cc.js.getset(Node.prototype, '_sgNode', function () { - return this.__sgNode; - }, function (value) { - this.__sgNode = value; - if (this._touchListener) { - this._touchListener.retain(); - cc.eventManager.removeListener(this._touchListener); - cc.eventManager.addListener(this._touchListener, this); - this._touchListener.release(); - } - if (this._mouseListener) { - this._mouseListener.retain(); - cc.eventManager.removeListener(this._mouseListener); - cc.eventManager.addListener(this._mouseListener, this); - this._mouseListener.release(); - } - }, true); + cc.js.getset(Node.prototype, '_sgNode', + function () { + return this.__sgNode; + }, + function (value) { + this.__sgNode = value; + if (this._touchListener) { + this._touchListener.retain(); + cc.eventManager.removeListener(this._touchListener); + cc.eventManager.addListener(this._touchListener, this); + this._touchListener.release(); + } + if (this._mouseListener) { + this._mouseListener.retain(); + cc.eventManager.removeListener(this._mouseListener); + cc.eventManager.addListener(this._mouseListener, this); + this._mouseListener.release(); + } + }, + true + ); } /** diff --git a/cocos2d/core/components/CCComponentInSG.js b/cocos2d/core/components/CCComponentInSG.js index 7ae842a276..6c6e85723c 100644 --- a/cocos2d/core/components/CCComponentInSG.js +++ b/cocos2d/core/components/CCComponentInSG.js @@ -20,12 +20,19 @@ var ComponentInSG = cc.Class({ }, ctor: function () { - this._sgNode = null; + /** + * Reference to the instance of _ccsg.Node + * If it is possible to return null from your overloaded _createSgNode, + * then you should always check for null before using this property. + * + * @property {_ccsg.Node} _sgNode + * @private + */ + this._sgNode = this._createSgNode(); }, onLoad: function () { - var sgNode = this._createSgNode(); - this._sgNode = sgNode; + var sgNode = this._sgNode; this._initSgNode(); this._appendSgNode(sgNode); if ( !this.node._sizeProvider ) { diff --git a/cocos2d/core/components/CCLabel.js b/cocos2d/core/components/CCLabel.js index 4de8c0cab3..8144b47587 100644 --- a/cocos2d/core/components/CCLabel.js +++ b/cocos2d/core/components/CCLabel.js @@ -50,10 +50,7 @@ var Label = cc.Class({ multiline: true, tooltip: 'i18n:COMPONENT.label.string', notify: function () { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.setString(this.string); - } + this._sgNode.setString(this.string); } }, @@ -66,10 +63,7 @@ var Label = cc.Class({ type: HorizontalAlign, tooltip: 'i18n:COMPONENT.label.horizontal_align', notify: function () { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.setHorizontalAlign( this.horizontalAlign ); - } + this._sgNode.setHorizontalAlign( this.horizontalAlign ); }, animatable: false }, @@ -83,10 +77,7 @@ var Label = cc.Class({ type: VerticalAlign, tooltip: 'i18n:COMPONENT.label.vertical_align', notify: function () { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.setVerticalAlign( this.verticalAlign ); - } + this._sgNode.setVerticalAlign( this.verticalAlign ); }, animatable: false }, @@ -98,19 +89,12 @@ var Label = cc.Class({ */ fontSize: { get: function(){ - var sgNode = this._sgNode; - if(sgNode){ - this._fontSize = sgNode.getFontSize(); - } + this._fontSize = this._sgNode.getFontSize(); return this._fontSize; }, set: function(value){ this._fontSize = value; - - var sgNode = this._sgNode; - if(sgNode){ - sgNode.setFontSize(value); - } + this._sgNode.setFontSize(value); }, tooltip: 'i18n:COMPONENT.label.font_size', }, @@ -119,19 +103,13 @@ var Label = cc.Class({ lineHeight: { get: function(){ - var sgNode = this._sgNode; - if(sgNode){ - this._lineHeight = sgNode.getLineHeight(); - } + this._lineHeight = this._sgNode.getLineHeight(); return this._lineHeight; }, set: function(value){ this._lineHeight = value; - var sgNode = this._sgNode; - if(sgNode){ - sgNode.setLineHeight(value); - } + this._sgNode.setLineHeight(value); }, tooltip: 'i18n:COMPONENT.label.line_height', }, @@ -144,10 +122,7 @@ var Label = cc.Class({ type: Overflow, tooltip: 'i18n:COMPONENT.label.overflow', notify: function () { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.setOverflow(this.overflow); - } + this._sgNode.setOverflow(this.overflow); }, animatable: false }, @@ -159,19 +134,12 @@ var Label = cc.Class({ */ enableWrapText: { get: function(){ - var sgNode = this._sgNode; - if(sgNode){ - this._enableWrapText = sgNode.isWrapTextEnabled(); - } + this._enableWrapText = this._sgNode.isWrapTextEnabled(); return this._enableWrapText; }, set: function(value){ this._enableWrapText = value; - - var sgNode = this._sgNode; - if(sgNode){ - sgNode.enableWrapText(value); - } + this._sgNode.enableWrapText(value); }, animatable: false, tooltip: 'i18n:COMPONENT.label.wrap', @@ -186,10 +154,7 @@ var Label = cc.Class({ url: cc.Font, tooltip: 'i18n:COMPONENT.label.file', notify: function () { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.setFontFileOrFamily(this.file); - } + this._sgNode.setFontFileOrFamily(this.file); }, animatable: false }, @@ -202,20 +167,14 @@ var Label = cc.Class({ */ useSystemFont: { get: function(){ - var sgNode = this._sgNode; - if(sgNode){ - this._isSystemFontUsed = sgNode.isSystemFontUsed(); - } + this._isSystemFontUsed = this._sgNode.isSystemFontUsed(); return this._isSystemFontUsed; }, set: function(value){ - var sgNode = this._sgNode; this._isSystemFontUsed = value; - if(value){ - if(sgNode){ - this.file = ""; - sgNode.setSystemFontUsed(value); - } + if (value) { + this.file = ""; + this._sgNode.setSystemFontUsed(value); } }, @@ -227,10 +186,7 @@ var Label = cc.Class({ // enableRichText: { // default: false, // notify: function () { - // var sgNode = this._sgNode; - // if (sgNode) { - // sgNode.enableRichText = this.enableRichText; - // } + // this._sgNode.enableRichText = this.enableRichText; // } // } diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index 9adcd86795..37840bd1b0 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -90,12 +90,10 @@ var Sprite = cc.Class({ set: function (value, force) { var lastSprite = this._spriteFrame; this._spriteFrame = value; - if (this._sgNode) { - this._applySpriteFrame(lastSprite); - // color cleared after reset texture, should reapply color - this._sgNode.setColor(this.node._color); - this._sgNode.setOpacity(this.node._opacity); - } + this._applySpriteFrame(lastSprite); + // color cleared after reset texture, should re-apply color + this._sgNode.setColor(this.node._color); + this._sgNode.setOpacity(this.node._opacity); }, type: cc.SpriteFrame, }, @@ -175,9 +173,6 @@ var Sprite = cc.Class({ localSize: { get: function () { var sgNode = this._sgNode; - if (!sgNode) { - return cc.size(0, 0); - } return cc.size(sgNode.width, sgNode.height); }, visible: false, diff --git a/cocos2d/particle/CCParticleSystem.js b/cocos2d/particle/CCParticleSystem.js index a54f655f01..230a616868 100644 --- a/cocos2d/particle/CCParticleSystem.js +++ b/cocos2d/particle/CCParticleSystem.js @@ -75,13 +75,11 @@ var properties = { default: true, editorOnly: true, notify: CC_EDITOR && function () { - if (this._sgNode) { - this._sgNode.resetSystem(); - if ( !this.preview ) { - this._sgNode.stopSystem(); - } - cc.engine.repaintInEditMode(); + this._sgNode.resetSystem(); + if ( !this.preview ) { + this._sgNode.stopSystem(); } + cc.engine.repaintInEditMode(); }, animatable: false }, @@ -102,18 +100,16 @@ var properties = { } if (this._custom !== value) { this._custom = value; - if (this._sgNode) { - if (value) { - this._applyCustoms(); - } - else { - this._applyFile(); - } - cc.engine.repaintInEditMode(); - //if (CC_EDITOR) { - // self.preview = self.preview; - //} + if (value) { + this._applyCustoms(); } + else { + this._applyFile(); + } + cc.engine.repaintInEditMode(); + //if (CC_EDITOR) { + // self.preview = self.preview; + //} } }, animatable: false @@ -135,7 +131,7 @@ var properties = { set: function (value, force) { if (this._file !== value || (CC_EDITOR && force)) { this._file = value; - if (this._sgNode && value) { + if (value) { this._applyFile(); cc.engine.repaintInEditMode(); //if (CC_EDITOR) { @@ -164,10 +160,7 @@ var properties = { }, set: function (value) { this._texture = value; - - if (this._sgNode) { - this._sgNode.texture = value ? cc.textureCache.addImage( value ) : null; - } + this._sgNode.texture = value ? cc.textureCache.addImage( value ) : null; }, url: cc.Texture2D }, @@ -177,12 +170,10 @@ var properties = { */ particleCount: { get: function () { - return this._sgNode ? this._sgNode.particleCount : 0; + return this._sgNode.particleCount; }, set: function (value) { - if (this._sgNode) { - this._sgNode.particleCount = value; - } + this._sgNode.particleCount = value; }, visible: false }, @@ -459,18 +450,14 @@ var CustomProps = (function () { if (cc.isChildClassOf(type, cc.ValueType)) { propDef.set = function (value) { this[internalProp] = new type(value); - if (this._sgNode) { - this._sgNode[prop] = value; - } + this._sgNode[prop] = value; }; } else if (CC_EDITOR && typeof defaultValue === "number") { propDef.set = function (value) { this[internalProp] = value; if (!isNaN(value)) { - if (this._sgNode) { - this._sgNode[prop] = value; - } + this._sgNode[prop] = value; } else { cc.error('The new %s must not be NaN', prop); @@ -480,9 +467,7 @@ var CustomProps = (function () { else { propDef.set = function (value) { this[internalProp] = value; - if (this._sgNode) { - this._sgNode[prop] = value; - } + this._sgNode[prop] = value; }; } })(prop, DefaultValues[prop]); @@ -609,7 +594,7 @@ var ParticleSystem = cc.Class({ onFocusInEditor: CC_EDITOR && function () { this._focused = true; - if (this.preview && this._sgNode) { + if (this.preview) { this._sgNode.resetSystem(); var self = this; @@ -630,7 +615,7 @@ var ParticleSystem = cc.Class({ onLostFocusInEditor: CC_EDITOR && function () { this._focused = false; - if (this.preview && this._sgNode) { + if (this.preview) { this._sgNode.resetSystem(); this._sgNode.stopSystem(); this._sgNode.update(); @@ -672,12 +657,7 @@ var ParticleSystem = cc.Class({ * @return {Boolean} */ addParticle: function () { - if (this._sgNode) { - return this._sgNode.addParticle(); - } - else { - return false; - } + return this._sgNode.addParticle(); }, /** @@ -685,9 +665,7 @@ var ParticleSystem = cc.Class({ * @method stopSystem */ stopSystem: function () { - if (this._sgNode) { - this._sgNode.stopSystem(); - } + this._sgNode.stopSystem(); }, /** @@ -695,9 +673,7 @@ var ParticleSystem = cc.Class({ * @method resetSystem */ resetSystem: function () { - if (this._sgNode) { - this._sgNode.resetSystem(); - } + this._sgNode.resetSystem(); }, /** @@ -724,9 +700,7 @@ var ParticleSystem = cc.Class({ if (texture) { this._texture = texture.url; } - if (this._sgNode) { - this._sgNode.setDisplayFrame(spriteFrame); - } + this._sgNode.setDisplayFrame(spriteFrame); }, /** @@ -739,42 +713,37 @@ var ParticleSystem = cc.Class({ if (texture instanceof cc.Texture2D) { this._texture = texture.url; } - if (this._sgNode) { - this._sgNode.setTextureWithRect(texture, rect); - } + this._sgNode.setTextureWithRect(texture, rect); }, // PRIVATE METHODS _applyFile: function (done) { - var node = this._sgNode; - if (!node) { - return; - } + var sgNode = this._sgNode; var file = this._file; if (file) { var self = this; cc.loader.load(file, function (err, results) { if (err) throw err; - node.particleCount = 0; + sgNode.particleCount = 0; - var active = node.isActive(); - node.initWithFile(file); + var active = sgNode.isActive(); + sgNode.initWithFile(file); // recover sgNode properties if (!active) { - node.stopSystem(); + sgNode.stopSystem(); } - var sourcePos = node.getPosition(); + var sourcePos = sgNode.getPosition(); if (CC_EDITOR && (sourcePos.x !== 0 || sourcePos.y !== 0)) { cc.log('Discard sourcePosition: %s from "%s", you can set position in the node directly.', sourcePos, cc.path.basename(file)); } - node.setPosition(0, 0); + sgNode.setPosition(0, 0); - node.autoRemoveOnFinish = self._autoRemoveOnFinish; + sgNode.autoRemoveOnFinish = self._autoRemoveOnFinish; // @@ -786,44 +755,38 @@ var ParticleSystem = cc.Class({ }, _applyCustoms: function () { - var node = this._sgNode; - if (!node) { - return; - } - - var active = node.isActive(); + var sgNode = this._sgNode; + var active = sgNode.isActive(); for (var i = 0; i < CustomProps.length; i++) { var prop = CustomProps[i]; - node[prop] = this['_' + prop]; + sgNode[prop] = this['_' + prop]; } if (this._texture) { - node.texture = cc.textureCache.addImage(this._texture); + sgNode.texture = cc.textureCache.addImage(this._texture); } // recover sgNode properties if (!active) { - node.stopSystem(); + sgNode.stopSystem(); } - node.autoRemoveOnFinish = this._autoRemoveOnFinish; + sgNode.autoRemoveOnFinish = this._autoRemoveOnFinish; }, _applyAutoRemove: function () { var sgNode = this._sgNode; - if (sgNode) { - var autoRemove = this._autoRemoveOnFinish; - sgNode.autoRemoveOnFinish = autoRemove; - if (autoRemove) { - cc.assert(!sgNode.onExit); - var self = this; - sgNode.onExit = function () { - _ccsg.Node.prototype.onExit.call(this); - self.node.destroy(); - }; - } - else if (sgNode.hasOwnProperty('onExit')) { - sgNode.onExit = _ccsg.Node.prototype.onExit; - } + var autoRemove = this._autoRemoveOnFinish; + sgNode.autoRemoveOnFinish = autoRemove; + if (autoRemove) { + cc.assert(!sgNode.onExit); + var self = this; + sgNode.onExit = function () { + _ccsg.Node.prototype.onExit.call(this); + self.node.destroy(); + }; + } + else if (sgNode.hasOwnProperty('onExit')) { + sgNode.onExit = _ccsg.Node.prototype.onExit; } } }); From da5905a98d842955f1c561920c0c88cfd89bbd29 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 27 Jan 2016 16:42:45 +0800 Subject: [PATCH 2/2] make it likely to initialize comp._sgNode just in constructor --- cocos2d/core/components/CCEditBox.js | 88 +++++++++------------------- 1 file changed, 27 insertions(+), 61 deletions(-) diff --git a/cocos2d/core/components/CCEditBox.js b/cocos2d/core/components/CCEditBox.js index 67ac8ecaeb..4b6ed3735c 100644 --- a/cocos2d/core/components/CCEditBox.js +++ b/cocos2d/core/components/CCEditBox.js @@ -51,10 +51,7 @@ var EditBox = cc.Class({ string: { default: '', notify: function() { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.string = this.string; - } + this._sgNode.string = this.string; } }, @@ -67,11 +64,9 @@ var EditBox = cc.Class({ type: cc.SpriteFrame, notify: function() { var sgNode = this._sgNode; - if (sgNode) { - var backgroundSprite = sgNode.getBackgroundSprite(); - backgroundSprite.setSpriteFrame(this.backgroundImage); - backgroundSprite.setContentSize(sgNode.getContentSize()); - } + var backgroundSprite = sgNode.getBackgroundSprite(); + backgroundSprite.setSpriteFrame(this.backgroundImage); + backgroundSprite.setContentSize(sgNode.getContentSize()); } }, @@ -85,10 +80,7 @@ var EditBox = cc.Class({ default: KeyboardReturnType.DEFAULT, type: KeyboardReturnType, notify: function() { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.returnType = this.returnType; - } + this._sgNode.returnType = this.returnType; } }, @@ -101,10 +93,7 @@ var EditBox = cc.Class({ default: InputFlag.INITIAL_CAPS_ALL_CHARACTERS, type: InputFlag, notify: function() { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.inputFlag = this.inputFlag; - } + this._sgNode.inputFlag = this.inputFlag; } }, @@ -118,10 +107,7 @@ var EditBox = cc.Class({ default: InputMode.ANY, type: InputMode, notify: function() { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.inputMode = this.inputMode; - } + this._sgNode.inputMode = this.inputMode; } }, @@ -132,10 +118,7 @@ var EditBox = cc.Class({ fontSize: { default: 20, notify: function() { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.fontSize = this.fontSize; - } + this._sgNode.fontSize = this.fontSize; } }, @@ -146,10 +129,7 @@ var EditBox = cc.Class({ fontColor: { default: cc.Color.WHITE, notify: function() { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.fontColor = this.fontColor; - } + this._sgNode.fontColor = this.fontColor; } }, @@ -160,10 +140,7 @@ var EditBox = cc.Class({ placeHolder: { default: 'Enter text here...', notify: function() { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.placeHolder = this.placeHolder; - } + this._sgNode.placeHolder = this.placeHolder; } }, @@ -174,10 +151,7 @@ var EditBox = cc.Class({ placeHolderFontSize: { default: 20, notify: function() { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.placeHolderFontSize = this.placeHolderFontSize; - } + this._sgNode.placeHolderFontSize = this.placeHolderFontSize; } }, @@ -188,10 +162,7 @@ var EditBox = cc.Class({ placeHolderFontColor: { default: cc.Color.GRAY, notify: function() { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.placeHolderFontColor = this.placeHolderFontColor; - } + this._sgNode.placeHolderFontColor = this.placeHolderFontColor; } }, @@ -202,10 +173,7 @@ var EditBox = cc.Class({ maxLength: { default: 20, notify: function() { - var sgNode = this._sgNode; - if (sgNode) { - sgNode.maxLength = this.maxLength; - } + this._sgNode.maxLength = this.maxLength; } }, @@ -251,25 +219,23 @@ var EditBox = cc.Class({ _initSgNode: function() { var sgNode = this._sgNode; - if (sgNode) { - var bgSprite = new cc.Scale9Sprite(this.backgroundImage); - sgNode.initWithSizeAndBackgroundSprite(this.node.getContentSize(), bgSprite); - sgNode.setContentSize(this.node.getContentSize()); + var bgSprite = new cc.Scale9Sprite(this.backgroundImage); + sgNode.initWithSizeAndBackgroundSprite(this.node.getContentSize(), bgSprite); + sgNode.setContentSize(this.node.getContentSize()); - sgNode.inputMode = this.inputMode; - sgNode.maxLength = this.maxLength; + sgNode.inputMode = this.inputMode; + sgNode.maxLength = this.maxLength; - sgNode.string = this.string; - sgNode.fontSize = this.fontSize; - sgNode.fontColor = this.fontColor; - sgNode.placeHolder = this.placeHolder; - sgNode.placeHolderFontSize = this.placeHolderFontSize; - sgNode.placeHolderFontColor = this.placeHolderFontColor; - sgNode.inputFlag = this.inputFlag; - sgNode.returnType = this.returnType; + sgNode.string = this.string; + sgNode.fontSize = this.fontSize; + sgNode.fontColor = this.fontColor; + sgNode.placeHolder = this.placeHolder; + sgNode.placeHolderFontSize = this.placeHolderFontSize; + sgNode.placeHolderFontColor = this.placeHolderFontColor; + sgNode.inputFlag = this.inputFlag; + sgNode.returnType = this.returnType; - sgNode.setDelegate(this); - } + sgNode.setDelegate(this); }, _handleComponentEvent: function(events, text) {