diff --git a/cocos2d/core/CCDirector.js b/cocos2d/core/CCDirector.js index de7056bd87..c1dc2639d5 100644 --- a/cocos2d/core/CCDirector.js +++ b/cocos2d/core/CCDirector.js @@ -796,35 +796,56 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ * @param {String} uuid - the uuid of the scene asset to load * @param {Function} [onLaunched] * @param {Function} [onUnloaded] + * @param {Boolean} [dontRunScene] - Just download and initialize the scene but will not launch it, + * only take effect in the Editor. * @private */ - _loadSceneByUuid: function (uuid, onLaunched, onUnloaded) { + _loadSceneByUuid: function (uuid, onLaunched, onUnloaded, dontRunScene) { + if (CC_EDITOR) { + if (typeof onLaunched === 'boolean') { + dontRunScene = onLaunched; + onLaunched = null; + } + if (typeof onUnloaded === 'boolean') { + dontRunScene = onUnloaded; + onUnloaded = null; + } + } //cc.AssetLibrary.unloadAsset(uuid); // force reload cc.AssetLibrary.loadAsset(uuid, function (error, sceneAsset) { var self = cc.director; self._loadingScene = ''; - var scene; if (error) { error = 'Failed to load scene: ' + error; cc.error(error); - if (CC_DEV) { - console.assert(false, error); - } } else { if (sceneAsset instanceof cc.SceneAsset) { - scene = sceneAsset.scene; + var scene = sceneAsset.scene; scene._id = sceneAsset._uuid; scene._name = sceneAsset._name; - self.runSceneImmediate(scene, onUnloaded, onLaunched); + if (CC_EDITOR) { + if (!dontRunScene) { + self.runSceneImmediate(scene, onUnloaded, onLaunched); + } + else { + scene._load(); + if (onLaunched) { + onLaunched(null, scene); + } + } + } + else { + self.runSceneImmediate(scene, onUnloaded, onLaunched); + } + return; } else { error = 'The asset ' + uuid + ' is not a scene'; cc.error(error); - scene = null; } } - if (error && onLaunched) { + if (onLaunched) { onLaunched(error); } }); diff --git a/cocos2d/core/assets/CCAsset.js b/cocos2d/core/assets/CCAsset.js index 7e5462744b..904d0469cc 100644 --- a/cocos2d/core/assets/CCAsset.js +++ b/cocos2d/core/assets/CCAsset.js @@ -102,6 +102,7 @@ cc.Asset = cc.Class({ * 在 lite 版的 Fireball 里,raw asset 并不仅仅是在 properties 里声明了 rawType 才有, * 而是每个 asset 都能指定自己的 raw file url。这些 url 就存在 _rawFiles 字段中。 * AssetLibrary 并不会帮你加载这些 url,除非你声明了 rawType。 + * 在 Creator 里,_rawFiles 保留了下来,为了复用 cocos 引擎原有实现,直接用 _rawFiles 来加载 Asset 在 import 之前的源文件。 * * @property _rawFiles * @type {String[]} @@ -152,8 +153,8 @@ cc.Asset = cc.Class({ * Create a new node using this asset in the scene.
* If this type of asset dont have its corresponding node type, this method should be null. * !#zh - * 使用该资产在场景中创建一个新节点。
- * 如果这类资产没有相应的节点类型,该方法应该是空的。 + * 使用该资源在场景中创建一个新节点。
+ * 如果这类资源没有相应的节点类型,该方法应该是空的。 * @method createNode * @param {Function} callback * @param {String} callback.error - null or the error info @@ -170,7 +171,17 @@ cc.Asset = cc.Class({ */ _setRawFiles: function (rawFiles) { this._rawFiles = rawFiles.length > 0 ? rawFiles : null; - } + }, + + /** + * Preload raw files when loading scene. + * + * @method _preloadRawFiles + * @param {Function} callback + * @param {Error} callback.error + * @private + */ + _preloadRawFiles: null, }); module.exports = cc.Asset; diff --git a/cocos2d/core/assets/CCBitmapFont.js b/cocos2d/core/assets/CCBitmapFont.js index bae8a009e1..2842259221 100644 --- a/cocos2d/core/assets/CCBitmapFont.js +++ b/cocos2d/core/assets/CCBitmapFont.js @@ -46,6 +46,12 @@ var BitmapFont = cc.Class({ fontSize: { default: -1 } + }, + + _preloadRawFiles: function (done) { + // preload fnt file. (texture already loaded in properties) + var fntUrl = this.rawUrl; + cc.loader.load({ id: fntUrl, ignoreMaxConcurrency: true }, done); } }); diff --git a/cocos2d/core/components/CCMask.js b/cocos2d/core/components/CCMask.js index 861b4301c4..8081439251 100644 --- a/cocos2d/core/components/CCMask.js +++ b/cocos2d/core/components/CCMask.js @@ -322,8 +322,10 @@ if (CC_JSB) { Mask.prototype.__superOnDestroy = Base.prototype.onDestroy; Mask.prototype.onDestroy = function () { this.__superOnDestroy(); - this._clippingStencil.release(); - this._clippingStencil = null; + if (this._clippingStencil) { + this._clippingStencil.release(); + this._clippingStencil = null; + } }; } diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index 73c83a2193..31b2d2e0f2 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -25,7 +25,7 @@ var NUMBER_OF_GATHERED_TOUCHES_FOR_MOVE_SPEED = 5; var OUT_OF_BOUNDARY_BREAKING_FACTOR = 0.05; -var EPSILON = 1e-7; +var EPSILON = 1e-4; var MOVEMENT_FACTOR = 0.7; var quintEaseOut = function(time) { diff --git a/cocos2d/core/event-manager/CCEventManager.js b/cocos2d/core/event-manager/CCEventManager.js index 871cb52d05..f75f08542e 100644 --- a/cocos2d/core/event-manager/CCEventManager.js +++ b/cocos2d/core/event-manager/CCEventManager.js @@ -695,6 +695,17 @@ cc.eventManager = { return a - b; }, + /** + * !#en Query whether the specified event listener id has been added. + * !#zh 查询指定的事件 ID 是否存在 + * @method hasEventListener + * @param {String|Number} listenerID - The listener id. + * @return {Boolean} true or false + */ + hasEventListener: function (listenerID) { + return !!this._getListeners(listenerID); + }, + /** * !#en *

diff --git a/cocos2d/core/event-manager/CCSystemEvent.js b/cocos2d/core/event-manager/CCSystemEvent.js index 881dedbd5a..a0ab1f4ee4 100644 --- a/cocos2d/core/event-manager/CCSystemEvent.js +++ b/cocos2d/core/event-manager/CCSystemEvent.js @@ -81,47 +81,55 @@ var SystemEvent = cc.Class({ this._super(type, callback, target, useCapture); // Keyboard - if (!keyboardListener && (type === EventType.KEY_DOWN || type === EventType.KEY_UP)) { - keyboardListener = cc.EventListener.create({ - event: cc.EventListener.KEYBOARD, - onKeyPressed: function (keyCode, event) { - event.type = EventType.KEY_DOWN; - if (CC_JSB) { - event.keyCode = keyCode; - event.isPressed = true; + if (type === EventType.KEY_DOWN || type === EventType.KEY_UP) { + if (!keyboardListener) { + keyboardListener = cc.EventListener.create({ + event: cc.EventListener.KEYBOARD, + onKeyPressed: function (keyCode, event) { + event.type = EventType.KEY_DOWN; + if (CC_JSB) { + event.keyCode = keyCode; + event.isPressed = true; + } + cc.systemEvent.dispatchEvent(event); + }, + onKeyReleased: function (keyCode, event) { + event.type = EventType.KEY_UP; + if (CC_JSB) { + event.keyCode = keyCode; + event.isPressed = false; + } + cc.systemEvent.dispatchEvent(event); } - cc.systemEvent.dispatchEvent(event); - }, - onKeyReleased: function (keyCode, event) { - event.type = EventType.KEY_UP; - if (CC_JSB) { - event.keyCode = keyCode; - event.isPressed = false; - } - cc.systemEvent.dispatchEvent(event); - } - }); - cc.eventManager.addListener(keyboardListener, 1); + }); + } + if (!cc.eventManager.hasEventListener(cc._EventListenerKeyboard.LISTENER_ID)) { + cc.eventManager.addListener(keyboardListener, 1); + } } // Acceleration - if (!accelerationListener && type === EventType.DEVICEMOTION) { - accelerationListener = cc.EventListener.create({ - event: cc.EventListener.ACCELERATION, - callback: function (accelEvent, event) { - event.type = EventType.DEVICEMOTION; - // fix android acc values are opposite - if (!CC_JSB && cc.sys.os === cc.sys.OS_ANDROID && - cc.sys.browserType !== cc.sys.BROWSER_TYPE_MOBILE_QQ) { - event.acc = cc.p(-accelEvent.x, -accelEvent.y); + if (type === EventType.DEVICEMOTION) { + if (accelerationListener) { + accelerationListener = cc.EventListener.create({ + event: cc.EventListener.ACCELERATION, + callback: function (accelEvent, event) { + event.type = EventType.DEVICEMOTION; + // fix android acc values are opposite + if (!CC_JSB && cc.sys.os === cc.sys.OS_ANDROID && + cc.sys.browserType !== cc.sys.BROWSER_TYPE_MOBILE_QQ) { + event.acc = cc.p(-accelEvent.x, -accelEvent.y); + } + else { + event.acc = cc.p(accelEvent.x, accelEvent.y); + } + cc.systemEvent.dispatchEvent(event); } - else { - event.acc = cc.p(accelEvent.x, accelEvent.y); - } - cc.systemEvent.dispatchEvent(event); - } - }); - cc.eventManager.addListener(accelerationListener, 1); + }); + } + if (!cc.eventManager.hasEventListener(cc._EventListenerAcceleration.LISTENER_ID)) { + cc.eventManager.addListener(accelerationListener, 1); + } } }, diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index 10520d0288..cf95a2a4b8 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -1140,8 +1140,8 @@ cc.BMFontHelper = { _setBMFontFile: function(filename, textureUrl) { if (filename) { this._fontHandle = filename; - var self = this; if (this._labelType === _ccsg.Label.Type.BMFont) { + var self = this; this._resetBMFont(); cc.loader.load(this._fontHandle, function(err, config) { diff --git a/cocos2d/core/load-pipeline/uuid-loader.js b/cocos2d/core/load-pipeline/uuid-loader.js index d997fed09a..f71854de3c 100644 --- a/cocos2d/core/load-pipeline/uuid-loader.js +++ b/cocos2d/core/load-pipeline/uuid-loader.js @@ -92,6 +92,15 @@ function loadDepends (pipeline, item, asset, tdInfo, deferredLoadRawAssetsInRunt propList.push(tdInfo.rawProp); depends.push(item.url); } + // preload raw files + if (asset._preloadRawFiles) { + var finalCallback = callback; + callback = function () { + asset._preloadRawFiles(function (err) { + finalCallback(err || null, asset); + }); + }; + } // fast path if (depends.length === 0) { asset._uuid = uuid; diff --git a/cocos2d/core/platform/CCClass.js b/cocos2d/core/platform/CCClass.js index be43538d63..e38cc19b87 100644 --- a/cocos2d/core/platform/CCClass.js +++ b/cocos2d/core/platform/CCClass.js @@ -42,7 +42,7 @@ var TYPO_TO_CORRECT = CC_DEV && { constructor: 'ctor' }; -var INVALID_STATICS = CC_DEV && ['name', '__ctors__', '__props__', 'arguments', 'call', 'apply', 'caller', +var INVALID_STATICS_DEV = CC_DEV && ['name', '__ctors__', '__props__', 'arguments', 'call', 'apply', 'caller', 'length', 'prototype']; var deferredInitializer = { @@ -305,7 +305,7 @@ function doDefine (className, baseClass, mixins, constructor, options) { // mixin statics (this will also copy editor attributes for component) for (var p in mixin) - if (mixin.hasOwnProperty(p) && INVALID_STATICS.indexOf(p) < 0) + if (mixin.hasOwnProperty(p) && (!CC_DEV || INVALID_STATICS_DEV.indexOf(p) < 0)) fireClass[p] = mixin[p]; // mixin attributes @@ -832,7 +832,7 @@ function CCClass (options) { var staticPropName; if (CC_DEV) { for (staticPropName in statics) { - if (INVALID_STATICS.indexOf(staticPropName) !== -1) { + if (INVALID_STATICS_DEV.indexOf(staticPropName) !== -1) { cc.error('Cannot define %s.%s because static member name can not be "%s".', name, staticPropName, staticPropName); } diff --git a/cocos2d/core/platform/CCView.js b/cocos2d/core/platform/CCView.js index 6683c5ee33..48156c5076 100644 --- a/cocos2d/core/platform/CCView.js +++ b/cocos2d/core/platform/CCView.js @@ -442,9 +442,9 @@ var View = cc._Class.extend({ } this._antiAliasEnabled = enabled; if(cc._renderType === cc.game.RENDER_TYPE_WEBGL) { - var map = cc.loader._cache; - for (var key in map) { - var item = map[key]; + var cache = cc.loader._cache; + for (var key in cache) { + var item = cache[key]; var tex = item && item.content instanceof cc.Texture2D ? item.content : null; if (tex) { if (enabled) { diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index dabe8aaef0..30a0049e3d 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -790,12 +790,12 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, _onPreDestroy: function () { + cc.eventManager.removeListeners(this); if (CC_JSB) { this._sgNode.release(); this._sgNode._entity = null; this._sgNode = null; } - cc.eventManager.removeListeners(this); for (var i = 0, len = this.__eventTargets.length; i < len; ++i) { var target = this.__eventTargets[i]; target && target.targetOff(this); diff --git a/cocos2d/particle/CCParticleSystem.js b/cocos2d/particle/CCParticleSystem.js index 42358ac8e1..743fc8bdd0 100644 --- a/cocos2d/particle/CCParticleSystem.js +++ b/cocos2d/particle/CCParticleSystem.js @@ -918,6 +918,10 @@ var ParticleSystem = cc.Class({ // so use the texutreUuid instead of textureImageData if (content.textureUuid) { cc.AssetLibrary.queryAssetInfo(content.textureUuid, function (err, url, raw) { + if (err) { + cc.error(err); + return; + } self.texture = url; }); } diff --git a/cocos2d/particle/CCSGParticleSystem.js b/cocos2d/particle/CCSGParticleSystem.js index a27740750a..8f2a47b708 100644 --- a/cocos2d/particle/CCSGParticleSystem.js +++ b/cocos2d/particle/CCSGParticleSystem.js @@ -1339,8 +1339,10 @@ _ccsg.ParticleSystem = _ccsg.Node.extend({ locModeA.tangentialAccelVar = (pszTmp) ? parseFloat(pszTmp) : 0; // rotation is dir - var locRotationIsDir = locValueForKey("rotationIsDir", dictionary).toLowerCase(); - locModeA.rotationIsDir = (locRotationIsDir != null && (locRotationIsDir === "true" || locRotationIsDir === "1")); + var locRotationIsDir = locValueForKey("rotationIsDir", dictionary); + locRotationIsDir = ("" + locRotationIsDir).toLowerCase(); + locModeA.rotationIsDir = (locRotationIsDir === "true" || locRotationIsDir === "1"); + } else if (this.emitterMode === _ccsg.ParticleSystem.Mode.RADIUS) { // or Mode B: radius movement var locModeB = this.modeB; diff --git a/jsb/jsb-director.js b/jsb/jsb-director.js index 88a5463a10..4d55bc3b97 100644 --- a/jsb/jsb-director.js +++ b/jsb/jsb-director.js @@ -328,35 +328,56 @@ cc.js.mixin(cc.director, { * @param {String} uuid - the uuid of the scene asset to load * @param {Function} [onLaunched] * @param {Function} [onUnloaded] + * @param {Boolean} [dontRunScene] - Just download and initialize the scene but will not launch it, + * only take effect in the Editor. * @private */ - _loadSceneByUuid: function (uuid, onLaunched, onUnloaded) { + _loadSceneByUuid: function (uuid, onLaunched, onUnloaded, dontRunScene) { + if (CC_EDITOR) { + if (typeof onLaunched === 'boolean') { + dontRunScene = onLaunched; + onLaunched = null; + } + if (typeof onUnloaded === 'boolean') { + dontRunScene = onUnloaded; + onUnloaded = null; + } + } //cc.AssetLibrary.unloadAsset(uuid); // force reload cc.AssetLibrary.loadAsset(uuid, function (error, sceneAsset) { var self = cc.director; self._loadingScene = ''; - var scene; if (error) { error = 'Failed to load scene: ' + error; cc.error(error); - if (CC_DEV) { - console.assert(false, error); - } } else { if (sceneAsset instanceof cc.SceneAsset) { - scene = sceneAsset.scene; + var scene = sceneAsset.scene; scene._id = sceneAsset._uuid; scene._name = sceneAsset._name; - self.runSceneImmediate(scene, onUnloaded, onLaunched); + if (CC_EDITOR) { + if (!dontRunScene) { + self.runSceneImmediate(scene, onUnloaded, onLaunched); + } + else { + scene._load(); + if (onLaunched) { + onLaunched(null, scene); + } + } + } + else { + self.runSceneImmediate(scene, onUnloaded, onLaunched); + } + return; } else { error = 'The asset ' + uuid + ' is not a scene'; cc.error(error); - scene = null; } } - if (error && onLaunched) { + if (onLaunched) { onLaunched(error); } });