Improve pause & resume logic

This commit is contained in:
pandamicro
2016-08-01 12:26:35 +08:00
parent 61f4e9ab23
commit 15670df9ec
2 changed files with 35 additions and 10 deletions
+8 -4
View File
@@ -389,8 +389,12 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
getZEye: null,
/**
* !#en Pause the director's ticker.
* !#zh 暂停正在运行的场景,该暂停只会停止 Scheduler,但是不会停止渲染和 UI 响应。
* !#en Pause the director's ticker, only involve the game logic execution.
* It won't pause the rendering process nor the event manager.
* If you want to pause the entier game including rendering, audio and event,
* please use {{#crossLink "Game.pause"}}cc.game.pause{{/crossLink}}
* !#zh 暂停正在运行的场景,该暂停只会停止游戏逻辑执行,但是不会停止渲染和 UI 响应。
* 如果想要更彻底得暂停游戏,包含渲染,音频和事件,请使用 {{#crossLink "Game.pause"}}cc.game.pause{{/crossLink}}。
* @method pause
*/
pause: function () {
@@ -818,8 +822,8 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
},
/**
* !#en Resume director after pause, if the current scene is not paused, nothing will happen.
* !#zh 恢复暂停场景,恢复 Scheduler,如果当前场景没有暂停将没任何事情发生。
* !#en Resume game logic execution after pause, if the current scene is not paused, nothing will happen.
* !#zh 恢复暂停场景的游戏逻辑,如果当前场景没有暂停将没任何事情发生。
* @method resume
*/
resume: function () {
+27 -6
View File
@@ -29,6 +29,8 @@ if (!(CC_EDITOR && Editor.isMainProcess)) {
View = require('./platform/CCView');
}
var _isMusicPlaying = false;
/**
* !#en An object to boot the game.
* !#zh 包含游戏主体信息并负责驱动游戏的游戏对象。
@@ -241,15 +243,26 @@ var game = /** @lends cc.game# */{
},
/**
* !#en Pause the game,pause main loop.
* !#zh 暂停游戏,暂停的是整个主循环。
* !#en Pause the game main loop. This will pause:
* game logic execution, rendering process, event manager, background music and all audio effects.
* This is different with cc.director.pause which only pause the game logic execution.
* !#zh 暂停游戏主循环。包含:游戏逻辑,渲染,事件处理,背景音乐和所有音效。这点和只暂停游戏逻辑的 cc.director.pause 不同。
* @method pause
*/
pause: function () {
if (this._paused) return;
this._paused = true;
// Pause audio engine
cc.audioEngine && cc.audioEngine._pausePlaying();
if (cc.audioEngine) {
_isMusicPlaying = cc.audioEngine.isMusicPlaying();
cc.audioEngine.stopAllEffects();
cc.audioEngine.pauseMusic();
}
// Pause event
var scene = cc.director.getScene() || cc.director.getRunningScene();
if (scene) {
cc.eventManager.pauseTarget(scene, true);
}
// Pause main loop
if (this._intervalId)
window.cancelAnimationFrame(this._intervalId);
@@ -257,15 +270,23 @@ var game = /** @lends cc.game# */{
},
/**
* !#en Resume the game from pause.
* !#zh 继续游戏,继续的是整个主循环。
* !#en Resume the game from pause. This will resume:
* game logic execution, rendering process, event manager, background music and all audio effects.
* !#zh 恢复游戏主循环。包含:游戏逻辑,渲染,事件处理,背景音乐和所有音效。
* @method resume
*/
resume: function () {
if (!this._paused) return;
this._paused = false;
// Resume audio engine
cc.audioEngine && cc.audioEngine._resumePlaying();
if (cc.audioEngine && _isMusicPlaying) {
cc.audioEngine.resumeMusic();
}
// Resume event
var scene = cc.director.getScene() || cc.director.getRunningScene();
if (scene) {
cc.eventManager.resumeTarget(scene, true);
}
// Resume main loop
this._runMainLoop();
},