Skip to content

Commit 836c9cc

Browse files
author
SeanLin
committed
Merge pull request cocos2d#561 from dingpinglv/iss1361_PortDirector
fixed cocos2d#1361 add page visibility event to fixed delta time mistake
2 parents 7de85fc + 1ccd63e commit 836c9cc

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

cocos2d/CCDirector.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,24 @@ cc.Director = cc.Class.extend(/** @lends cc.Director# */{
169169
_watcherSender:null,
170170

171171
_currTimeValue:null,
172+
_isBlur:false,
172173

173174
/**
174175
* Constructor
175176
*/
176177
ctor:function () {
177178
this._currTimeValue = new cc.timeval();
178179
this._lastUpdate = new cc.timeval();
179-
var selfPointer = this;
180-
window.addEventListener("focus",function(){
181-
selfPointer._lastUpdate = cc.Time.gettimeofdayCocos2d(selfPointer._lastUpdate);
182-
}, false);
180+
if(!cc.isAddedHiddenEvent){
181+
var selfPointer = this;
182+
window.addEventListener("focus",function(){
183+
selfPointer._lastUpdate = cc.Time.gettimeofdayCocos2d(selfPointer._lastUpdate);
184+
}, false);
185+
}
186+
},
187+
188+
_resetLastUpdate:function(){
189+
this._lastUpdate = cc.Time.gettimeofdayCocos2d(this._lastUpdate);
183190
},
184191

185192
/**

cocos2d/platform/CCApplication.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ if (!window.console) {
122122
};
123123
}
124124

125+
126+
cc.isAddedHiddenEvent = false;
127+
125128
/**
126129
* <p>
127130
* setup game main canvas,renderContext,gameDiv and drawingUtil with argument <br/>
@@ -197,8 +200,37 @@ cc.setup = function (el, width, height) {
197200
}
198201
}, true);
199202
*/
203+
204+
var hidden, visibilityChange;
205+
if (typeof document.hidden !== "undefined") {
206+
hidden = "hidden";
207+
visibilityChange = "visibilitychange";
208+
} else if (typeof document.mozHidden !== "undefined") {
209+
hidden = "mozHidden";
210+
visibilityChange = "mozvisibilitychange";
211+
} else if (typeof document.msHidden !== "undefined") {
212+
hidden = "msHidden";
213+
visibilityChange = "msvisibilitychange";
214+
} else if (typeof document.webkitHidden !== "undefined") {
215+
hidden = "webkitHidden";
216+
visibilityChange = "webkitvisibilitychange";
217+
}
218+
219+
function handleVisibilityChange() {
220+
if (!document[hidden])
221+
cc.Director.getInstance()._resetLastUpdate();
222+
}
223+
224+
if (typeof document.addEventListener === "undefined" ||
225+
typeof hidden === "undefined") {
226+
cc.isAddedHiddenEvent = false;
227+
} else {
228+
cc.isAddedHiddenEvent = true;
229+
document.addEventListener(visibilityChange, handleVisibilityChange, false);
230+
}
200231
};
201232

233+
202234
/**
203235
* Run main loop of game engine
204236
* @class
@@ -244,7 +276,7 @@ cc.Application = cc.Class.extend(/** @lends cc.Application# */{
244276
return 0;
245277
}
246278
// TODO, need to be fixed.
247-
if (window.requestAnimFrame && this._animationInterval == 1/60) {
279+
if (window.requestAnimFrame && this._animationInterval == 1 / 60) {
248280
var callback = function () {
249281
cc.Director.getInstance().mainLoop();
250282
window.requestAnimFrame(callback);

0 commit comments

Comments
 (0)