diff --git a/cocos2d/core/components/CCComponent.js b/cocos2d/core/components/CCComponent.js index 8ea95a835d..27a27790aa 100644 --- a/cocos2d/core/components/CCComponent.js +++ b/cocos2d/core/components/CCComponent.js @@ -374,7 +374,7 @@ var Component = cc.Class({ */ enabledInHierarchy: { get: function () { - return this._objFlags & IsOnEnableCalled; + return (this._objFlags & IsOnEnableCalled) > 0; }, visible: false }, diff --git a/cocos2d/core/components/CCComponentEventHandler.js b/cocos2d/core/components/CCComponentEventHandler.js index cbaf23cbe7..85275a290c 100644 --- a/cocos2d/core/components/CCComponentEventHandler.js +++ b/cocos2d/core/components/CCComponentEventHandler.js @@ -80,6 +80,7 @@ cc.Component.EventHandler = cc.Class({ /** * @method emitEvents * @param {Component.EventHandler[]} events + * @param {Any} ...params * @statics */ emitEvents: function(events) { @@ -88,7 +89,7 @@ cc.Component.EventHandler = cc.Class({ var event = events[i]; if (! event instanceof cc.Component.EventHandler) continue; - event.emit(Array.prototype.slice.call(arguments,1)); + event.emit(Array.prototype.slice.call(arguments, 1)); } } }, diff --git a/cocos2d/core/components/missing-script.js b/cocos2d/core/components/missing-script.js index 82bafac5bf..23e947db3e 100644 --- a/cocos2d/core/components/missing-script.js +++ b/cocos2d/core/components/missing-script.js @@ -25,10 +25,23 @@ var JS = cc.js; +/* + * A temp fallback to contain the original serialized data which can not be loaded. + */ +var MissingClass = cc.Class({ + name: 'cc.MissingClass', + properties: { + // the serialized data for original object + _$erialized: { + default: null, + visible: false, + editorOnly: true + } + }, +}); + /* * A temp fallback to contain the original component which can not be loaded. - * Actually, this class will be used whenever a class failed to deserialize, - * regardless of whether it is child class of component. */ var MissingScript = cc.Class({ name: 'cc.MissingScript', @@ -84,13 +97,19 @@ var MissingScript = cc.Class({ * @param {string} id * @return {function} constructor */ - safeFindClass: function (id) { + safeFindClass: function (id, data) { var cls = JS._getClassById(id); if (cls) { return cls; } if (id) { - return MissingScript; + cc.deserialize.reportMissingClass(id); + if (data.node && (CC_EDITOR && Editor.UuidUtils.isUuid(id))) { + return MissingScript; + } + else { + return MissingClass; + } } return null; } diff --git a/cocos2d/core/platform/CCObject.js b/cocos2d/core/platform/CCObject.js index ac25c74b5f..7abf85d342 100644 --- a/cocos2d/core/platform/CCObject.js +++ b/cocos2d/core/platform/CCObject.js @@ -245,14 +245,14 @@ var deferredDestroyTimer = null; /** * !#en * Destroy this Object, and release all its own references to other objects.
+ * Actual object destruction will delayed until before rendering. *
- * After destroy, this CCObject is not usable any more.
- * You can use cc.isValid(obj) (or obj.isValid if obj is non-nil) to check whether the object is destroyed before - * accessing it. + * After destroy, this CCObject is not usable any more. + * You can use cc.isValid(obj) to check whether the object is destroyed before accessing it. * !#zh * 销毁该对象,并释放所有它对其它对象的引用。
- * 销毁后,CCObject 不再可用。
- * 您可以在访问对象之前使用 cc.isValid(obj)(或 obj.isValid 如果 obj 不为 null)来检查对象是否已被销毁。 + * 销毁后,CCObject 不再可用。您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。 + * 实际销毁操作会延迟到当前帧渲染前执行。 * @method destroy * @return {Boolean} whether it is the first time the destroy being called * @example diff --git a/cocos2d/core/platform/deserialize.js b/cocos2d/core/platform/deserialize.js index f9da036042..d0ac823e65 100644 --- a/cocos2d/core/platform/deserialize.js +++ b/cocos2d/core/platform/deserialize.js @@ -27,8 +27,6 @@ var JS = require('./js'); var CCObject = require('./CCObject'); var Attr = require('./attribute'); -var ENABLE_TARGET = CC_DEV; - // HELPERS /** @@ -68,20 +66,16 @@ var Details = function () { if (CC_DEV) { /** - * 用户可以指定一个在反序列化结束时会被触发的回调,该回调会传回反序列化时统计到的所有解析过的字段。 + * 用户可以指定一个在反序列化过程中会被触发的回调,该回调会在反序列化之前调用,并且传回反序列化时解析到的字段。 * NOTE: + * - only available in editor * - 会被传回的字段仅限于非 Asset 类型,并且如果字段值为 null 或 undefined,则可能不会被传回。 - * - 该回调在 DeserializeInfo 第一次调用 reset 时就会被清空。 - * @callback visitorInEditor - * @param {Object[]} objs - * @param {String[]} propNames - * @param {Object} _Deserializer + * @callback visit + * @param {Object} obj + * @param {String} propName * @private */ - this.visitorInEditor = null; - - this.visitObjList = []; - this.visitPropList = []; + this.visit = null; } }; /** @@ -94,18 +88,8 @@ Details.prototype.reset = function () { this.rawProp = ''; //this.rawObjList.length = 0; //this.rawPropList.length = 0; - - if (CC_DEV) { - this.visitorInEditor = null; - this.visitObjList.length = 0; - this.visitPropList.length = 0; - } }; if (CC_DEV) { - Details.prototype.visitLater = function (obj, propName) { - this.visitObjList.push(obj); - this.visitPropList.push(propName); - }; Details.prototype.assignAssetsBy = function (getter) { for (var i = 0, len = this.uuidList.length; i < len; i++) { var uuid = this.uuidList[i]; @@ -144,13 +128,11 @@ Details.prototype.push = function (obj, propName, uuid) { // IMPLEMENT OF DESERIALIZATION var _Deserializer = (function () { - ///** - // * @param {Boolean} isEditor - if false, "editorOnly" properties will be discarded - // */ - function _Deserializer(jsonObj, result, target, classFinder, customEnv) { + function _Deserializer(jsonObj, result, target, classFinder, customEnv, ignoreEditorOnly) { this._classFinder = classFinder; - if (ENABLE_TARGET) { + if (CC_DEV) { this._target = target; + this._ignoreEditorOnly = ignoreEditorOnly; } this._idList = []; this._idObjList = []; @@ -166,7 +148,7 @@ var _Deserializer = (function () { for (var i = 0; i < refCount; i++) { if (jsonArray[i]) { var mainTarget; - if (ENABLE_TARGET) { + if (CC_DEV) { mainTarget = (i === 0 && target); } this.deserializedList[i] = _deserializeObject(this, jsonArray[i], mainTarget); @@ -194,14 +176,9 @@ var _Deserializer = (function () { // dereference _dereference(this); - - // call visitor after all properties initialized - if (CC_DEV) { - this._callVisitorInEditor(); - } } - var _dereference = function (self) { + function _dereference (self) { // 这里不采用遍历反序列化结果的方式,因为反序列化的结果如果引用到复杂的外部库,很容易堆栈溢出。 var deserializedList = self.deserializedList; var idPropList = self._idPropList; @@ -212,18 +189,9 @@ var _Deserializer = (function () { var id = idList[i]; idObjList[i][propName] = deserializedList[id]; } - }; - - if (CC_DEV) { - _Deserializer.prototype._callVisitorInEditor = function () { - var result = this.result; - if (result.visitorInEditor) { - result.visitorInEditor(result.visitObjList, result.visitPropList, this); - } - }; } - // 和 _deserializeObject 不同的地方在于会判断 id 和 uuid + // 和 _deserializeObject 不同的地方在于会判断 id 和 uuid _Deserializer.prototype._deserializeObjField = function (obj, jsonObj, propName, target) { var id = jsonObj.__id__; if (typeof id === 'undefined') { @@ -243,14 +211,14 @@ var _Deserializer = (function () { this.result.uuidPropList.push(propName); } else { - if (ENABLE_TARGET) { + if (CC_DEV) { obj[propName] = _deserializeObject(this, jsonObj, target && target[propName]); } else { obj[propName] = _deserializeObject(this, jsonObj); } - if (CC_DEV && this.result.visitorInEditor) { - this.result.visitLater(obj, propName); + if (CC_DEV && this.result.visit) { + this.result.visit(obj, propName); } } } @@ -264,8 +232,8 @@ var _Deserializer = (function () { this._idObjList.push(obj); this._idPropList.push(propName); } - if (CC_DEV && this.result.visitorInEditor) { - this.result.visitLater(obj, propName); + if (CC_DEV && this.result.visit) { + this.result.visit(obj, propName); } } }; @@ -277,14 +245,14 @@ var _Deserializer = (function () { if (typeof prop !== 'object') { if (propName !== '__type__'/* && k != '__id__'*/) { instance[propName] = prop; - if (CC_DEV && self.result.visitorInEditor) { - self.result.visitLater(instance, propName); + if (CC_DEV && self.result.visit) { + self.result.visit(instance, propName); } } } else { if (prop) { - if (ENABLE_TARGET) { + if (CC_DEV) { self._deserializeObjField(instance, prop, propName, self._target && instance); } else { @@ -313,7 +281,7 @@ var _Deserializer = (function () { } else { if (prop) { - if (ENABLE_TARGET) { + if (CC_DEV) { self._deserializeObjField(instance, prop, propName, self._target && instance); } else { @@ -329,22 +297,25 @@ var _Deserializer = (function () { //--self.stackCounter; } + var RAW_TYPE = Attr.DELIMETER + 'rawType'; + var EDITOR_ONLY = Attr.DELIMETER + 'editorOnly'; + var SERIALIZABLE = Attr.DELIMETER + 'serializable'; + function _deserializeFireClass(self, obj, serialized, klass, target) { - var DELIMETER = Attr.DELIMETER; var props = klass.__props__; var attrs = Attr.getClassAttrs(klass); for (var p = 0; p < props.length; p++) { var propName = props[p]; - // assume all prop in __props__ must have attr - var rawType = attrs[propName + DELIMETER + 'rawType']; + var rawType = attrs[propName + RAW_TYPE]; if (!rawType) { - if (!CC_DEV && attrs[propName + DELIMETER + 'editorOnly']) { + if (((CC_EDITOR && self._ignoreEditorOnly) || (!CC_EDITOR && CC_DEV && !CC_TEST)) + && attrs[propName + EDITOR_ONLY]) { var mayUsedInPersistRoot = (obj instanceof cc.Node && propName === '_id'); if ( !mayUsedInPersistRoot ) { - continue; // skip editor only if not editor + continue; // skip editor only if in preview } } - if (attrs[propName + DELIMETER + 'serializable'] === false) { + if (attrs[propName + SERIALIZABLE] === false) { continue; // skip nonSerialized } var prop = serialized[propName]; @@ -356,7 +327,7 @@ var _Deserializer = (function () { } else { if (prop) { - if (ENABLE_TARGET) { + if (CC_DEV) { self._deserializeObjField(obj, prop, propName, target && obj); } else { @@ -389,8 +360,8 @@ var _Deserializer = (function () { // * @param {Object} serialized - The obj to deserialize, must be non-nil // * @param {Object} [target=null] // */ - var _deserializeObject = function (self, serialized, target) { - var propName, prop; + function _deserializeObject (self, serialized, target) { + var prop; var obj = null; // the obj to return var klass = null; if (serialized.__type__) { @@ -398,18 +369,16 @@ var _Deserializer = (function () { // Type Object (including CCClass) var type = serialized.__type__; - klass = self._classFinder(type); + klass = self._classFinder(type, serialized); if (!klass) { - if (CC_EDITOR && Editor.UuidUtils.isUuid(type)) { - type = Editor.UuidUtils.decompressUuid(type); - cc.warn('Can not find script "%s"', type); - return null; + var noLog = self._classFinder === JS._getClassById; + if (noLog) { + cc.deserialize.reportMissingClass(type); } - cc.warn('Can not find class "%s"', type); return null; } - if (ENABLE_TARGET && target) { + if (CC_DEV && target) { // use target if ( !(target instanceof klass) ) { cc.warn('Type of target to deserialize not matched with data: target is %s, data is %s', @@ -441,14 +410,14 @@ var _Deserializer = (function () { // embedded primitive javascript object - obj = (ENABLE_TARGET && target) || {}; + obj = (CC_DEV && target) || {}; _deserializePrimitiveObject(self, obj, serialized); } else { // Array - if (ENABLE_TARGET && target) { + if (CC_DEV && target) { target.length = serialized.length; obj = target; } @@ -459,7 +428,7 @@ var _Deserializer = (function () { for (var i = 0; i < serialized.length; i++) { prop = serialized[i]; if (typeof prop === 'object' && prop) { - if (ENABLE_TARGET) { + if (CC_DEV) { self._deserializeObjField(obj, prop, '' + i, target && obj); } else { @@ -468,21 +437,18 @@ var _Deserializer = (function () { } else { obj[i] = prop; - - if (CC_DEV && self.result.visitorInEditor) { - self.result.visitLater(obj, '' + i); + if (CC_DEV && self.result.visit) { + self.result.visit(obj, '' + i); } } } } return obj; - }; + } return _Deserializer; })(); -// FACADE - /** * @module cc */ @@ -504,8 +470,9 @@ cc.deserialize = function (data, result, options) { var classFinder = (options && options.classFinder) || JS._getClassById; // 启用 createAssetRefs 后,如果有 url 属性则会被统一强制设置为 { uuid: 'xxx' },必须后面再特殊处理 var createAssetRefs = (options && options.createAssetRefs) || cc.sys.platform === cc.sys.EDITOR_CORE; - var target = ENABLE_TARGET && (options && options.target); - var customEnv = (options && options.customEnv); + var target = CC_DEV && (options && options.target); + var customEnv = options && options.customEnv; + var ignoreEditorOnly = options && options.ignoreEditorOnly; if (CC_EDITOR && Buffer.isBuffer(data)) { data = data.toString(); @@ -521,7 +488,7 @@ cc.deserialize = function (data, result, options) { result = new Details(); } cc.game._isCloning = true; - var deserializer = new _Deserializer(data, result, target, classFinder, customEnv); + var deserializer = new _Deserializer(data, result, target, classFinder, customEnv, ignoreEditorOnly); cc.game._isCloning = false; if (createAssetRefs) { @@ -537,3 +504,12 @@ cc.deserialize = function (data, result, options) { }; cc.deserialize.Details = Details; +cc.deserialize.reportMissingClass = function (id) { + if (CC_EDITOR && Editor.UuidUtils.isUuid(id)) { + id = Editor.UuidUtils.decompressUuid(id); + cc.warn('Can not find script "%s"', id); + } + else { + cc.warn('Can not find class "%s"', id); + } +}; diff --git a/cocos2d/core/utils/find.js b/cocos2d/core/utils/find.js index 986b1b9dff..fbcdd94df7 100644 --- a/cocos2d/core/utils/find.js +++ b/cocos2d/core/utils/find.js @@ -43,11 +43,21 @@ cc.find = module.exports = function (path, referenceNode) { if (!referenceNode) { var scene = cc.director.getScene(); if (!scene) { - cc.warn('Can not get current scene.'); + if (CC_DEV) { + cc.warn('Can not get current scene.'); + } + return null; + } + else if (CC_DEV && !scene.isValid) { + cc.warn('Scene is destroyed'); return null; } referenceNode = scene; } + else if (CC_DEV && !referenceNode.isValid) { + cc.warn('reference node is destroyed'); + return null; + } var match = referenceNode; var startIndex = (path[0] !== '/') ? 0 : 1; // skip first '/'