From 7a1fac062d31d4fa25bf3771ce4f321d55051ca2 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Sat, 12 Dec 2015 00:33:27 +0800 Subject: [PATCH] Fix issues for jsb runtime --- cocos2d/core/CCScene.js | 3 ++ cocos2d/core/components/CCCanvas.js | 4 +- cocos2d/core/components/CCComponentInSG.js | 1 + cocos2d/core/components/CCSpriteRenderer.js | 2 +- cocos2d/core/platform/deserialize.js | 4 ++ cocos2d/core/utils/base-node.js | 52 ++++++++++----------- cocos2d/core/utils/scene-graph-helper.js | 1 + gulpfile.js | 4 +- jsb_polyfill.js | 14 +++++- 9 files changed, 54 insertions(+), 31 deletions(-) diff --git a/cocos2d/core/CCScene.js b/cocos2d/core/CCScene.js index 6160bc2003..66c951d44f 100644 --- a/cocos2d/core/CCScene.js +++ b/cocos2d/core/CCScene.js @@ -37,6 +37,7 @@ cc.Scene = cc.Class({ ctor: function () { var sgNode = this._sgNode = new _ccsg.Scene(); + sgNode.retain(); sgNode.setAnchorPoint(0.0, 0.0); this._anchorPoint.x = 0.0; this._anchorPoint.y = 0.0; @@ -49,6 +50,8 @@ cc.Scene = cc.Class({ var children = this._children; var DontDestroy = cc.Object.Flags.DontDestroy; + this._sgNode.release(); + for (var i = 0, len = children.length; i < len; ++i) { var child = children[i]; if (child.isValid) { diff --git a/cocos2d/core/components/CCCanvas.js b/cocos2d/core/components/CCCanvas.js index 43e69bf397..14c5107897 100644 --- a/cocos2d/core/components/CCCanvas.js +++ b/cocos2d/core/components/CCCanvas.js @@ -145,7 +145,7 @@ var Canvas = cc.Class({ if (CC_EDITOR) { cc.engine.on('design-resolution-changed', this._thisOnResized); } - else { + else if (!cc.sys.isNative) { if (cc.sys.isMobile) { window.addEventListener('resize', this._thisOnResized); } @@ -168,7 +168,7 @@ var Canvas = cc.Class({ if (CC_EDITOR) { cc.engine.off('design-resolution-changed', this._thisOnResized); } - else { + else if (!cc.sys.isNative) { if (cc.sys.isMobile) { window.removeEventListener('resize', this._thisOnResized); } diff --git a/cocos2d/core/components/CCComponentInSG.js b/cocos2d/core/components/CCComponentInSG.js index 91ccf7e3bf..211dbb3a56 100644 --- a/cocos2d/core/components/CCComponentInSG.js +++ b/cocos2d/core/components/CCComponentInSG.js @@ -80,6 +80,7 @@ var ComponentInSG = cc.Class({ sgParent.addChild(sgNode); // retain immediately + // will be released in SceneGraphHelper.removeSgNode sgNode.retain(); this._sgNode = sgNode; } diff --git a/cocos2d/core/components/CCSpriteRenderer.js b/cocos2d/core/components/CCSpriteRenderer.js index 327d4bf803..1d5466e455 100644 --- a/cocos2d/core/components/CCSpriteRenderer.js +++ b/cocos2d/core/components/CCSpriteRenderer.js @@ -460,7 +460,7 @@ var SpriteRenderer = cc.Class({ }, _applySprite: function (sgNode, oldSprite) { - if (oldSprite) { + if (oldSprite && oldSprite.off) { oldSprite.off('load', this._applyCapInset, this); } if (!this._sprite) { return; } diff --git a/cocos2d/core/platform/deserialize.js b/cocos2d/core/platform/deserialize.js index 79b36b30b3..0d605f6422 100644 --- a/cocos2d/core/platform/deserialize.js +++ b/cocos2d/core/platform/deserialize.js @@ -414,6 +414,10 @@ var _Deserializer = (function () { else { // instantiate a new object obj = new klass(); + // Temporary solution + if (cc.sys.isNative && klass === cc.SpriteFrame) { + obj.retain(); + } } if (obj._deserialize) { diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index b9b3db7d46..706a0e0a72 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -180,7 +180,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, set: function (value) { this._skewX = value; - this._sgNode.skewX = value; + this._sgNode.setSkewX(value); } }, @@ -195,7 +195,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, set: function (value) { this._skewY = value; - this._sgNode._skewY = value; + this._sgNode.setSkewY(value); } }, @@ -210,7 +210,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, set: function (value) { this._localZOrder = value; - this._sgNode.zIndex = value; + this._sgNode.setLocalZOrder(value); } }, @@ -227,7 +227,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, set: function (value) { this._rotationX = this._rotationY = value; - this._sgNode.rotation = value; + this._sgNode.setRotation(value); }, tooltip: "The clockwise degrees of rotation relative to the parent" }, @@ -243,7 +243,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, set: function (value) { this._rotationX = value; - this._sgNode.rotationX = value; + this._sgNode.setRotationX(value); }, }, @@ -258,7 +258,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, set: function (value) { this._rotationY = value; - this._sgNode.rotationY = value; + this._sgNode.setRotationY(value); }, }, @@ -273,7 +273,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, set: function (value) { this._scaleX = value; - this._sgNode.scaleX = value; + this._sgNode.setScaleX(value); }, }, @@ -288,7 +288,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, set: function (value) { this._scaleY = value; - this._sgNode.scaleY = value; + this._sgNode.setScaleY(value); }, }, @@ -303,7 +303,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, set: function (value) { this._position.x = value; - this._sgNode.x = value; + this._sgNode.setPositionX(value); }, }, @@ -318,7 +318,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, set: function (value) { this._position.y = value; - this._sgNode.y = value; + this._sgNode.setPositionY(value); }, }, @@ -395,7 +395,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ set: function (value) { if (value !== this._contentSize.width) { if (this._sizeProvider) { - this._sizeProvider.setContentSize(new cc.Size(value, this._sizeProvider._getHeight())); + this._sizeProvider.setContentSize(value, this._sizeProvider._getHeight()); } this._contentSize.width = value; this.emit(SIZE_CHANGED); @@ -422,7 +422,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ set: function (value) { if (value !== this._contentSize.height) { if (this._sizeProvider) { - this._sizeProvider.setContentSize(new cc.Size(this._sizeProvider._getWidth(), value)); + this._sizeProvider.setContentSize(this._sizeProvider._getWidth(), value); } this._contentSize.height = value; this.emit(SIZE_CHANGED); @@ -445,7 +445,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, set: function (value) { this._ignoreAnchorPointForPosition = value; - this._sgNode.ignoreAnchor = value; + this._sgNode.ignoreAnchorPointForPosition(value); this._onAnchorChanged(); }, }, @@ -461,7 +461,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, set: function (value) { this._tag = value; - this._sgNode.tag = value; + this._sgNode.setTag(value); }, }, @@ -476,7 +476,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, set: function (value) { this._opacity = value; - this._sgNode.opacity = value; + this._sgNode.setOpacity(value); this._onColorChanged(); }, range: [0, 255] @@ -494,7 +494,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ set: function (value) { if (this._cascadeOpacityEnabled !== value) { this._cascadeOpacityEnabled = value; - this._sgNode.cascadeOpacity = value; + this._sgNode.setCascadeOpacityEnabled(value); this._onCascadeChanged(); } }, @@ -532,8 +532,9 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }); var sgNode = this._sgNode = new _ccsg.Node(); + sgNode.retain(); if (!cc.game._isCloning) { - sgNode.cascadeOpacity = true; + sgNode.setCascadeOpacityEnabled(true); } this._dirtyFlags = DirtyFlags.ALL; @@ -548,6 +549,11 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ this._sizeProvider = null; }, + destroy: function () { + this._sgNode.release(); + this._super(); + }, + // ABSTRACT INTERFACES // called when the node's parent changed @@ -587,12 +593,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ } }, - //Helper function used by `setLocalZOrder`. Don't use it unless you know what you are doing. - _setLocalZOrder: function (localZOrder) { - this._localZOrder = localZOrder; - this._sgNode._localZOrder = localZOrder; - }, - /** *

Defines the oder in which the nodes are renderer.
* Nodes that have a Global Z Order lower, are renderer first.
@@ -1085,9 +1085,9 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ return this._sgNode.convertToWorldSpaceAR(nodePoint); }, - _convertToWindowSpace: function (nodePoint) { - return this._sgNode._convertToWindowSpace(nodePoint); - }, + // _convertToWindowSpace: function (nodePoint) { + // return this._sgNode._convertToWindowSpace(nodePoint); + // }, /** * convenience methods which take a cc.Touch instead of cc.Vec2 diff --git a/cocos2d/core/utils/scene-graph-helper.js b/cocos2d/core/utils/scene-graph-helper.js index 592517e1d2..fbdfdeb11e 100644 --- a/cocos2d/core/utils/scene-graph-helper.js +++ b/cocos2d/core/utils/scene-graph-helper.js @@ -5,6 +5,7 @@ var SceneGraphUtils = { removeSgNode: function () { var node = this._sgNode; if (node) { + node.release(); if (node._parent) { node._parent.removeChild(node); } diff --git a/gulpfile.js b/gulpfile.js index d07147dcb6..c5d310c741 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -20,7 +20,9 @@ paths = { JSBSkipModules: [ '../../cocos2d/core/CCGame', '../../cocos2d/core/textures/CCTexture2D', - '../../cocos2d/core/sprites/CCSpriteFrame' + '../../cocos2d/core/sprites/CCSpriteFrame', + '../../cocos2d/core/platform/CCLoader', + '../../cocos2d/core/platform/CCLoaders' ], test: { diff --git a/jsb_polyfill.js b/jsb_polyfill.js index dc9b9b20c6..f6ec83772b 100644 --- a/jsb_polyfill.js +++ b/jsb_polyfill.js @@ -1,3 +1,4 @@ +'use strict'; cc.ParticleSystem.Mode = cc.Enum({ /** The gravity mode (A mode) */ @@ -317,7 +318,7 @@ cc.js.mixin(cc.path, { //removing all ../ do { oldUrl = url; - url = url.replace(this.normalizeRE, ""); + url = url.replace(this.normalizeRE, ''); } while(oldUrl.length !== url.length); return url; }, @@ -356,6 +357,17 @@ cc.EventTarget.call(cc.director); cc.js.addon(cc.director, cc.EventTarget.prototype); // cc.Label +cc._Label = cc.Label; +cc.Label = function (string, fontHandle, type) { + var label; + if (type === cc.Label.Type.TTF) { + label = cc._Label.createWithSystemFont(string, fontHandle, 40); + } + else if (type === cc.Label.Type.BMFont) { + label = cc._Label.createWithBMFont(fontHandle, string); + } + return label; +}; cc.Label.Type = cc.Enum({ TTF: 0, BMFont: 1