diff --git a/cocos2d/core/components/CCButton.js b/cocos2d/core/components/CCButton.js index 7e8ffca642..e0e529c71f 100644 --- a/cocos2d/core/components/CCButton.js +++ b/cocos2d/core/components/CCButton.js @@ -290,22 +290,6 @@ var Button = cc.Class({ this.node.on(cc.Node.EventType.MOUSE_LEAVE, this._onMouseMoveOut, this); }, - _handleClickEvent: function () { - var events = this.clickEvents; - for (var i = 0, l = events.length; i < l; i++) { - var event = events[i]; - var target = event.target; - if (!target) continue; - - var comp = target.getComponent(event.component); - if (!comp) continue; - - var handler = comp[event.handler]; - if (!handler) continue; - handler.call(comp) - } - }, - _cancelButtonClick: function(){ this._pressed = false; }, @@ -341,7 +325,7 @@ var Button = cc.Class({ _onTouchEnded: function () { if (this._pressed) { - this._handleClickEvent(); + cc.Component.EventHandler.emitEvents(this.clickEvents); } this._pressed = false; this._updateState(); diff --git a/cocos2d/core/components/CCComponentEventHandler.js b/cocos2d/core/components/CCComponentEventHandler.js index 85113b8b54..0820405bbe 100644 --- a/cocos2d/core/components/CCComponentEventHandler.js +++ b/cocos2d/core/components/CCComponentEventHandler.js @@ -35,5 +35,40 @@ cc.Component.EventHandler = cc.Class({ handler: { default: '', } + }, + + statics: { + /** + * Emit events with params + * @method emitEvents + * @param {Array} events + * @param {*} params + */ + emitEvents: function(events, params) { + for (var i = 0, l = events.length; i < l; i++) { + var event = events[i]; + if (! event instanceof cc.Component.EventHandler) continue; + + event.emit(params); + } + } + }, + + /** + * Emit event with params + * @method emit + * @param {*} params + */ + emit: function(params) { + var target = this.target; + if (!cc.isValid(target)) return; + + var comp = target.getComponent(this.component); + if (!cc.isValid(comp)) return; + + var handler = comp[this.handler]; + if (typeof(handler) !== 'function') return; + + handler.call(comp, params); } }); diff --git a/cocos2d/core/components/CCEditBox.js b/cocos2d/core/components/CCEditBox.js index 7851e0ac74..a2095c6991 100644 --- a/cocos2d/core/components/CCEditBox.js +++ b/cocos2d/core/components/CCEditBox.js @@ -200,7 +200,7 @@ var EditBox = cc.Class({ /** * The event handler to be called when EditBox edit ends. - * @property {cc.Component.EventHandler} editingDidEnd + * @property {cc.Component.EventHandler} editingDidEnded */ editingDidEnded: { default: [], @@ -241,34 +241,16 @@ var EditBox = cc.Class({ sgNode.setDelegate(this); }, - _handleComponentEvent: function(events, text) { - for (var i = 0, l = events.length; i < l; i++) { - var event = events[i]; - var target = event.target; - if (!cc.isValid(target)) continue; - - var comp = target.getComponent(event.component); - if (!cc.isValid(comp)) continue; - - var handler = comp[event.handler]; - if (!handler) continue; - handler.call(comp, text); - } - }, - editBoxEditingDidBegan: function() { - var events = this.editingDidBegan; - this._handleComponentEvent(events); + cc.Component.EventHandler.emitEvents(this.editingDidBegan); }, editBoxEditingDidEnded: function() { - var events = this.editingDidEnded; - this._handleComponentEvent(events); + cc.Component.EventHandler.emitEvents(this.editingDidEnded); }, editBoxTextChanged: function(editBox, text) { - var events = this.textChanged; - this._handleComponentEvent(events, text); + cc.Component.EventHandler.emitEvents(this.textChanged, text); }, }); diff --git a/cocos2d/tilemap/CCTiledMap.js b/cocos2d/tilemap/CCTiledMap.js index 65dd0c990e..421946ecf4 100644 --- a/cocos2d/tilemap/CCTiledMap.js +++ b/cocos2d/tilemap/CCTiledMap.js @@ -37,6 +37,11 @@ var TiledMap = cc.Class({ }, properties: { + _isLoading: { + default: false, + serializable: false, + }, + _tiledMap: { default: null, serializable: false, @@ -63,6 +68,15 @@ var TiledMap = cc.Class({ } }, url: cc.TiledMapAsset + }, + + /** + * The event handler to be called when the map is loaded. + * @property {cc.Component.EventHandler} mapLoaded + */ + mapLoaded: { + default: [], + type: cc.Component.EventHandler, } }, @@ -164,24 +178,44 @@ var TiledMap = cc.Class({ }, /** - * Initializes the instance of cc.TiledMap with tmxFile + * Initializes the instance of cc.TiledMap with tmxFile. + * The mapLoaded events will be emitted when the map is loaded. * @method initWithTMXFile * @param {String} tmxFile - * @return {Boolean} Whether the initialization was successful. */ initWithTMXFile:function (tmxFile) { - return this._tiledMap.initWithTMXFile(tmxFile); + this._tmxFile = tmxFile; }, /** - * Initializes the instance of cc.TiledMap with tmxString + * Initializes the instance of cc.TiledMap with tmxString. + * The mapLoaded events will be emitted when the map is loaded. * @method initWithXML * @param {String} tmxString * @param {String} resourcePath - * @return {Boolean} Whether the initialization was successful. */ initWithXML:function(tmxString, resourcePath){ - return this._tiledMap.initWithXML(tmxString, resourcePath); + // clear the tmx file + this._tmxFile = null; + + // preload textures & init the _tileMap + var sgNode = this._tiledMap; + var self = this; + var mapInfo = new cc.TMXMapInfo(tmxString, resourcePath); + if (!mapInfo) { + self._onMapLoaded(new Error('Parse map info failed.')); + return; + } + + self._isLoading = true; + this._preloadTextures(mapInfo, function(err, results) { + if (err) { + self._onMapLoaded(err); + } else { + sgNode.initWithXML(tmxString, resourcePath); + self._onMapLoaded(); + } + }); }, /** @@ -257,9 +291,9 @@ var TiledMap = cc.Class({ onEnable: function () { this.node._replaceSgNode(this._tiledMap); - if (this._tmxFile) { - // Add layer entities - this._addLayerEntities(); + if (this._tmxFile && ! this._isLoading) { + // refresh layer entities + this._refreshLayerEntities(); } this.node.on('child-added', this._childAdded, this); @@ -297,7 +331,24 @@ var TiledMap = cc.Class({ } }, + _preloadTextures: function(mapInfo, cb) { + var sets = mapInfo.getTilesets(); + if (sets) { + var textures = sets.map(function (set) { + return set.sourceImage; + }); + + cc.loader.load(textures, function (err) { + cb(err, textures); + }); + } + else { + if (cb) cb(); + } + }, + _preloadTmx: function(file, cb) { + var self = this; cc.loader.load(file, function (err, items) { if (err) { if (cb) cb(items.getError(file) || new Error('Unknown error')); @@ -305,23 +356,27 @@ var TiledMap = cc.Class({ } var mapInfo = new cc.TMXMapInfo(file); - var sets = mapInfo.getTilesets(); - - if (sets) { - var textures = sets.map(function (set) { - return set.sourceImage; - }); - - cc.loader.load(textures, function (err) { - cb(err, textures); - }); - } - else { - if (cb) cb(); + if (!mapInfo) { + cb(new Error('Parse map info failed.')); + return; } + + self._preloadTextures(mapInfo, cb); }); }, + _onMapLoaded: function(err) { + this._isLoading = false; + if (err) { + cc.Component.EventHandler.emitEvents(this.mapLoaded, err); + } else { + if (this._enabled) { + this._refreshLayerEntities(); + } + cc.Component.EventHandler.emitEvents(this.mapLoaded); + } + }, + _moveLayersInSgNode: function(sgNode) { var children = sgNode.getChildren(); var needRemove = []; @@ -338,20 +393,6 @@ var TiledMap = cc.Class({ } }, - _addLayerEntities: function() { - // add entity for the tmx layer - var layers = this._tiledMap.allLayers(); - for (var i = 0, n = layers.length; i < n; i++) { - var layer = layers[i]; - var name = layer.getLayerName(); - var node = new cc.Node(name); - var addedLayer = node.addComponent(cc.TiledLayer); - addedLayer._replaceSgNode(layer); - this.node.addChild(node); - node.setSiblingIndex(layer.getLocalZOrder()); - } - }, - _removeLayerEntities: function() { var i, n; var logicChildren = this.node.getChildren(); @@ -369,7 +410,7 @@ var TiledMap = cc.Class({ } }, - _initLayers: function() { + _refreshLayerEntities: function() { // get the layer names in scene graph. var layerNames = this._tiledMap.allLayers().map(function (layer) { return layer.getLayerName(); @@ -472,12 +513,13 @@ var TiledMap = cc.Class({ var file = this._tmxFile; var self = this; if (file) { + self._isLoading = true; this._preloadTmx(file, function (err, results) { - if (err) throw err; - - sgNode.initWithTMXFile(file); - if (self._enabled) { - self._initLayers(); + if (err) { + self._onMapLoaded(err); + } else { + sgNode.initWithTMXFile(file); + self._onMapLoaded(); } }); } else {