diff --git a/cocos2d/CCDirector.js b/cocos2d/CCDirector.js index 69cbfcc7ae..5f1f305a3b 100644 --- a/cocos2d/CCDirector.js +++ b/cocos2d/CCDirector.js @@ -169,6 +169,7 @@ cc.Director = cc.Class.extend(/** @lends cc.Director# */{ _watcherSender:null, _currTimeValue:null, + _isBlur:false, /** * Constructor @@ -176,10 +177,16 @@ cc.Director = cc.Class.extend(/** @lends cc.Director# */{ ctor:function () { this._currTimeValue = new cc.timeval(); this._lastUpdate = new cc.timeval(); - var selfPointer = this; - window.addEventListener("focus",function(){ - selfPointer._lastUpdate = cc.Time.gettimeofdayCocos2d(selfPointer._lastUpdate); - }, false); + if(!cc.isAddedHiddenEvent){ + var selfPointer = this; + window.addEventListener("focus",function(){ + selfPointer._lastUpdate = cc.Time.gettimeofdayCocos2d(selfPointer._lastUpdate); + }, false); + } + }, + + _resetLastUpdate:function(){ + this._lastUpdate = cc.Time.gettimeofdayCocos2d(this._lastUpdate); }, /** diff --git a/cocos2d/platform/CCApplication.js b/cocos2d/platform/CCApplication.js index ce0cf7e531..cc7414ed11 100644 --- a/cocos2d/platform/CCApplication.js +++ b/cocos2d/platform/CCApplication.js @@ -122,6 +122,9 @@ if (!window.console) { }; } + +cc.isAddedHiddenEvent = false; + /** *
* setup game main canvas,renderContext,gameDiv and drawingUtil with argument
@@ -197,8 +200,37 @@ cc.setup = function (el, width, height) {
}
}, true);
*/
+
+ var hidden, visibilityChange;
+ if (typeof document.hidden !== "undefined") {
+ hidden = "hidden";
+ visibilityChange = "visibilitychange";
+ } else if (typeof document.mozHidden !== "undefined") {
+ hidden = "mozHidden";
+ visibilityChange = "mozvisibilitychange";
+ } else if (typeof document.msHidden !== "undefined") {
+ hidden = "msHidden";
+ visibilityChange = "msvisibilitychange";
+ } else if (typeof document.webkitHidden !== "undefined") {
+ hidden = "webkitHidden";
+ visibilityChange = "webkitvisibilitychange";
+ }
+
+ function handleVisibilityChange() {
+ if (!document[hidden])
+ cc.Director.getInstance()._resetLastUpdate();
+ }
+
+ if (typeof document.addEventListener === "undefined" ||
+ typeof hidden === "undefined") {
+ cc.isAddedHiddenEvent = false;
+ } else {
+ cc.isAddedHiddenEvent = true;
+ document.addEventListener(visibilityChange, handleVisibilityChange, false);
+ }
};
+
/**
* Run main loop of game engine
* @class
@@ -244,7 +276,7 @@ cc.Application = cc.Class.extend(/** @lends cc.Application# */{
return 0;
}
// TODO, need to be fixed.
- if (window.requestAnimFrame && this._animationInterval == 1/60) {
+ if (window.requestAnimFrame && this._animationInterval == 1 / 60) {
var callback = function () {
cc.Director.getInstance().mainLoop();
window.requestAnimFrame(callback);