From 4dd2557e04536572c73aad0b2d1da949323dfb3f Mon Sep 17 00:00:00 2001 From: zhangbin Date: Mon, 25 Jul 2016 15:56:20 +0800 Subject: [PATCH] Support show object group of Tiled Map. --- cocos2d/tilemap/CCSGTMXObject.js | 44 +++++++++--- cocos2d/tilemap/CCTMXXMLParser.js | 5 +- cocos2d/tilemap/CCTiledMap.js | 4 +- cocos2d/tilemap/CCTiledObject.js | 100 -------------------------- cocos2d/tilemap/CCTiledObjectGroup.js | 44 ++++-------- 5 files changed, 52 insertions(+), 145 deletions(-) delete mode 100644 cocos2d/tilemap/CCTiledObject.js diff --git a/cocos2d/tilemap/CCSGTMXObject.js b/cocos2d/tilemap/CCSGTMXObject.js index 063a5e4dda..981877648f 100644 --- a/cocos2d/tilemap/CCSGTMXObject.js +++ b/cocos2d/tilemap/CCSGTMXObject.js @@ -5,11 +5,12 @@ _ccsg.TMXObjectImage = _ccsg.Sprite.extend(/** @lends cc.TMXObjectImage# */{ ctor:function (objInfo, mapInfo) { _ccsg.Sprite.prototype.ctor.call(this); + this._setProperties(objInfo); this.initWithMapInfo(objInfo, mapInfo); }, initWithMapInfo: function (objInfo, mapInfo) { - this.setObjectName(objInfo.name); + this._setObjectName(objInfo.name); this.id = objInfo.id; this.gid = objInfo.gid; this.type = objInfo.type; @@ -100,25 +101,52 @@ _ccsg.TMXObjectImage = _ccsg.Sprite.extend(/** @lends cc.TMXObjectImage# */{ _ccsg.TMXObjectShape = _ccsg.Node.extend(/** @lends cc.TMXObjectShape# */{ ctor:function (objInfo, mapInfo) { _ccsg.Node.prototype.ctor.call(this); - this.setObjectName(objInfo.name); + this._setProperties(objInfo); + this._setObjectName(objInfo.name); } }); -// methods for tmxObject -cc.TMXObjectHelper = { +/** + * !#en Renders the TMX object. + * !#zh 渲染 tmx object。 + * @class TMXObject + * @extends _ccsg.Node + */ +cc.TMXObject = { isTmxObject: true, offset: cc.p(0, 0), gid: 0, - _name: '', + name: '', type: '', id: 0, + _properties: null, + /** + * !#en Get the name of object + * !#zh 获取对象的名称 + * @method getObjectName + * @return {String} + */ getObjectName: function() { - return this._name; + return this.name; }, - setObjectName: function(name) { - this._name = name; + /** + * !#en Get the property of object + * !#zh 获取对象的属性 + * @method getProperty + * @return {Object} + */ + getProperty: function (propName) { + return this._properties[propName]; + }, + + _setObjectName: function(name) { + this.name = name; + }, + + _setProperties: function (props) { + this._properties = props; } }; diff --git a/cocos2d/tilemap/CCTMXXMLParser.js b/cocos2d/tilemap/CCTMXXMLParser.js index 7acfaddb49..f623c373ed 100644 --- a/cocos2d/tilemap/CCTMXXMLParser.js +++ b/cocos2d/tilemap/CCTMXXMLParser.js @@ -912,10 +912,7 @@ cc.TMXMapInfo = cc.SAXParser.extend(/** @lends cc.TMXMapInfo# */{ // visible var visibleAttr = selObj.getAttribute('visible'); - if (visibleAttr && parseInt(visibleAttr) === 0) - objectProp['visible'] = false; - else - objectProp['visible'] = true; + objectProp['visible'] = ! (visibleAttr && parseInt(visibleAttr) === 0); // image var gid = selObj.getAttribute('gid'); diff --git a/cocos2d/tilemap/CCTiledMap.js b/cocos2d/tilemap/CCTiledMap.js index 259815b1a2..e090315b7f 100644 --- a/cocos2d/tilemap/CCTiledMap.js +++ b/cocos2d/tilemap/CCTiledMap.js @@ -656,8 +656,8 @@ var TiledMap = cc.Class({ // the tmx object group should be updated existedGroups.push(child); var newSGGroup = this._sgNode.getObjectGroup(groupName); - tmxGroup.enabled = newSGGroup.isVisible(); tmxGroup._replaceSgNode(newSGGroup); + tmxGroup.enabled = newSGGroup.isVisible(); } } else { otherChildrenInfo.push({child: child, index: child.getSiblingIndex()}); @@ -729,10 +729,10 @@ var TiledMap = cc.Class({ cc.error('Add component TiledLayer into node failed.'); } - node.enabled = sgGroup.isVisible(); addedGroup._replaceSgNode(sgGroup); node.setSiblingIndex(sgGroup.getLocalZOrder()); node.setAnchorPoint(this.node.getAnchorPoint()); + addedGroup.enabled = sgGroup.isVisible(); } else { existedGroups[theIndex].setSiblingIndex(sgGroup.getLocalZOrder()); } diff --git a/cocos2d/tilemap/CCTiledObject.js b/cocos2d/tilemap/CCTiledObject.js deleted file mode 100644 index edd1477427..0000000000 --- a/cocos2d/tilemap/CCTiledObject.js +++ /dev/null @@ -1,100 +0,0 @@ -/**************************************************************************** - Copyright (c) 2016 Chukong Technologies Inc. - - http://www.cocos.com - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated engine source code (the "Software"), a limited, - worldwide, royalty-free, non-assignable, revocable and non-exclusive license - to use Cocos Creator solely to develop games on your target platforms. You shall - not use Cocos Creator software for developing other software or tools that's - used for developing games. You are not granted to publish, distribute, - sublicense, and/or sell copies of Cocos Creator. - - The software or tools in this License Agreement are licensed, not sold. - Chukong Aipu reserves all rights not expressly granted to you. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -var TiledObject = cc.Class({ - name: 'cc.TiledObject', - - // Inherits from the abstract class directly, - // because TiledLayer not create or maintains the sgNode by itself. - extends: cc._SGComponent, - - onEnable: function () { - if (this._sgNode) { - this._sgNode.setVisible(true); - } - }, - onDisable: function () { - if (this._sgNode) { - this._sgNode.setVisible(false); - } - }, - - onDestroy: function () { - if (this.node._sizeProvider === this._sgNode) { - this.node._sizeProvider = null; - } - }, - - _initSgNode: function () { - var sgNode = this._sgNode; - if (!sgNode) { - return; - } - if (!this.enabledInHierarchy) { - sgNode.setVisible(false); - } - this._registSizeProvider(); - var node = this.node; - sgNode.setAnchorPoint(node.getAnchorPoint()); - }, - - _replaceSgNode: function (sgNode) { - if (sgNode === this._sgNode) { - return; - } - - // Remove the sgNode before - this._removeSgNode(); - if (this.node._sizeProvider === this._sgNode) { - this.node._sizeProvider = null; - } - - if (sgNode && sgNode.isTmxObject) { - this._sgNode = sgNode; - if (CC_JSB) { - // retain the new sgNode, it will be released in _removeSgNode - sgNode.retain(); - } - - this._initSgNode(); - } else { - this._sgNode = null; - } - }, - - // The method will remove self component from the node, - // and try to remove the node from scene graph. - // It should only be invoked by cc.TiledMap - // DO NOT use it manually. - _tryRemoveNode: function() { - this.node.removeComponent(cc.TiledObject); - if (this.node._components.length === 1 && - this.node.getChildren().length === 0) { - this.node.removeFromParent(); - } - } -}); - -cc.TiledObject = module.exports = TiledObject; diff --git a/cocos2d/tilemap/CCTiledObjectGroup.js b/cocos2d/tilemap/CCTiledObjectGroup.js index 00e5cbb986..5bfe0c477f 100644 --- a/cocos2d/tilemap/CCTiledObjectGroup.js +++ b/cocos2d/tilemap/CCTiledObjectGroup.js @@ -22,6 +22,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ + +/** + * !#en Renders the TMX object group. + * !#zh 渲染 tmx object group。 + * @class TiledObjectGroup + * @extends _SGComponent + */ var TiledObjectGroup = cc.Class({ name: 'cc.TiledObjectGroup', @@ -51,12 +58,8 @@ var TiledObjectGroup = cc.Class({ if ( !sgNode ) { return; } - if ( !this.enabledInHierarchy ) { - sgNode.setVisible(false); - } this._registSizeProvider(); - var node = this.node; - sgNode.setAnchorPoint(node.getAnchorPoint()); + sgNode.setAnchorPoint(this.node.getAnchorPoint()); }, _replaceSgNode: function(sgNode) { @@ -195,13 +198,8 @@ var TiledObjectGroup = cc.Class({ * var object = tMXObjectGroup.getObject("Group"); */ getObject: function(objectName){ - var logicChildren = this.node.children; - for (var i = 0, n = logicChildren.length; i < n; i++) { - var child = logicChildren[i]; - var tmxObject = child.getComponent(cc.TiledObject); - if (tmxObject && tmxObject.getObjectName() === objectName) { - return tmxObject; - } + if (this._sgNode) { + return this._sgNode.getObject(objectName); } return null; @@ -216,17 +214,11 @@ var TiledObjectGroup = cc.Class({ * var objects = tMXObjectGroup.getObjects(); */ getObjects:function () { - var logicChildren = this.node.children; - var ret = []; - for (var i = 0, n = logicChildren.length; i < n; i++) { - var child = logicChildren[i]; - var tmxObject = child.getComponent(cc.TiledObject); - if (tmxObject) { - ret.push(tmxObject); - } + if (this._sgNode) { + return this._sgNode.getObjects(); } - return ret; + return []; }, // The method will remove self component from the node, @@ -237,16 +229,6 @@ var TiledObjectGroup = cc.Class({ // remove the component this.node.removeComponent(cc.TiledObjectGroup); - // remove the TMXObject - var groupChildren = this.node.getChildren(); - for (var j = groupChildren.length - 1; j >= 0; j--) { - var groupChild = groupChildren[j]; - var tmxObject = groupChild.getComponent(cc.TiledObject); - if (tmxObject) { - tmxObject._tryRemoveNode(); - } - } - // try to remove the object group if (this.node._components.length === 1 && this.node.getChildren().length === 0) {