diff --git a/cocos2d/core/CCGame.js b/cocos2d/core/CCGame.js index d4e43b5359..48a15ec531 100644 --- a/cocos2d/core/CCGame.js +++ b/cocos2d/core/CCGame.js @@ -36,19 +36,45 @@ if (!(CC_EDITOR && Editor.isMainProcess)) { */ var game = /** @lends cc.game# */{ + /** + * Event triggered when game hide to background. + * Please note that this event is not 100% guaranteed to be fired. + * @constant + * @type {String} + * @example + * cc.game.on(cc.game.EVENT_HIDE, function () { + * cc.audioEngine.pauseMusic(); + * cc.audioEngine.pauseAllEffects(); + * }); + */ EVENT_HIDE: "game_on_hide", + + /** + * Event triggered when game back to foreground + * Please note that this event is not 100% guaranteed to be fired. + * @constant + * @type {String} + */ EVENT_SHOW: "game_on_show", - EVENT_RESIZE: "game_on_resize", + + /** + * Event triggered after game inited, at this point all engine objects and game scripts are loaded + * @constant + * @type {String} + */ EVENT_GAME_INITED: "game_inited", + + /** + * Event triggered after renderer inited, at this point you will be able to use the render context + * @constant + * @type {String} + */ EVENT_RENDERER_INITED: "renderer_inited", RENDER_TYPE_CANVAS: 0, RENDER_TYPE_WEBGL: 1, RENDER_TYPE_OPENGL: 2, - _eventHide: null, - _eventShow: null, - _persistRootNodes: {}, _ignoreRemovePersistNode: null, @@ -672,12 +698,7 @@ var game = /** @lends cc.game# */{ }, _initEvents: function () { - var win = window, self = this, hidden, visibilityChange, _undef = "undefined"; - - this._eventHide = this._eventHide || new cc.Event.EventCustom(this.EVENT_HIDE); - this._eventHide.setUserData(this); - this._eventShow = this._eventShow || new cc.Event.EventCustom(this.EVENT_SHOW); - this._eventShow.setUserData(this); + var win = window, hidden, visibilityChange, _undef = "undefined"; // register system events if (this.config[this.CONFIG_KEY.registerSystemEvent]) @@ -698,12 +719,10 @@ var game = /** @lends cc.game# */{ } var onHidden = function () { - if (cc.eventManager && game._eventHide) - cc.eventManager.dispatchEvent(game._eventHide); + game.emit(game.EVENT_HIDE, game); }; var onShow = function () { - if (cc.eventManager && game._eventShow) - cc.eventManager.dispatchEvent(game._eventShow); + game.emit(game.EVENT_SHOW, game); }; if (hidden) { @@ -725,10 +744,10 @@ var game = /** @lends cc.game# */{ win.addEventListener("pageshow", onShow, false); } - cc.eventManager.addCustomListener(game.EVENT_HIDE, function () { + this.on(game.EVENT_HIDE, function () { game.pause(); }); - cc.eventManager.addCustomListener(game.EVENT_SHOW, function () { + this.on(game.EVENT_SHOW, function () { game.resume(); }); } diff --git a/cocos2d/core/event-manager/CCEventManager.js b/cocos2d/core/event-manager/CCEventManager.js index aad31ae417..09cf9e404c 100644 --- a/cocos2d/core/event-manager/CCEventManager.js +++ b/cocos2d/core/event-manager/CCEventManager.js @@ -122,7 +122,7 @@ cc.eventManager = { _isEnabled: false, _nodePriorityIndex: 0, - _internalCustomListenerIDs:[cc.game.EVENT_HIDE, cc.game.EVENT_SHOW], + _internalCustomListenerIDs:[], _setDirtyForNode: function (node) { // Mark the node dirty only when there is an event listener associated with it. diff --git a/extensions/ccui/uiwidgets/UIVideoPlayer.js b/extensions/ccui/uiwidgets/UIVideoPlayer.js index 84cb28553f..be21132e4b 100644 --- a/extensions/ccui/uiwidgets/UIVideoPlayer.js +++ b/extensions/ccui/uiwidgets/UIVideoPlayer.js @@ -262,7 +262,7 @@ ccui.VideoPlayer = ccui.Widget.extend(/** @lends ccui.VideoPlayer# */{ ccui.VideoPlayer.elements = []; ccui.VideoPlayer.pauseElements = []; -cc.eventManager.addCustomListener(cc.game.EVENT_HIDE, function () { +cc.game.on(cc.game.EVENT_HIDE, function () { var list = ccui.VideoPlayer.elements; for(var node, i=0; i