Skip to content

Commit 35e6117

Browse files
committed
Device.quirksMode is a boolean that informs you if the page is in strict (false) or quirks (true) mode.
Canvas.getOffset now runs a strict/quirks check and uses document.documentElement when calculating scrollTop and scrollLeft to avoid Chrome console warnings. The Time class now has three new methods: addEvent, repeatEvent and loopEvent. See the new Timer examples to show how to use them.
1 parent 67e2caa commit 35e6117

7 files changed

Lines changed: 179 additions & 62 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ Updates:
130130
* Added in prototype.constructor definitions to every class (thanks darkoverlordofdata)
131131
* Group.destroy has a new parameter: destroyChildren (boolean) which will optionally call the destroy method of all Group children.
132132
* Button.clearFrames method has been added.
133+
* Device.quirksMode is a boolean that informs you if the page is in strict (false) or quirks (true) mode.
134+
* Canvas.getOffset now runs a strict/quirks check and uses document.documentElement when calculating scrollTop and scrollLeft to avoid Chrome console warnings.
135+
* The Time class now has three new methods: addEvent, repeatEvent and loopEvent. See the new Timer examples to show how to use them.
133136

134137

135138
Bug Fixes:

examples/wip/timer simple.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload:
44
function preload() {
55

66
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
7+
game.load.image('sonic', 'assets/sprites/pangball.png');
78

89
}
910

@@ -13,33 +14,30 @@ function create() {
1314

1415
game.stage.backgroundColor = '#007236';
1516

16-
timer = game.time.create(1000, false);
17+
// Every second we will call the addSprite function. This will happen 10 times and then stop.
18+
// The final parameter is the one that will be sent to the addSprite function and in this case is the sprite key.
19+
game.time.repeatEvent(Phaser.Timer.SECOND, 10, addSprite, this, 'mushroom');
1720

18-
timer.repeat(1, 10);
19-
20-
timer.onEvent.add(addSprite, this);
21-
22-
timer.start();
23-
24-
// text = game.add.text(0, 0, "Text Above Sprites", { font: "64px Arial", fill: "#00bff3", align: "center" });
21+
// Every 1.5 seconds we will call the addSprite function. This will happen 5 times and then stop.
22+
game.time.repeatEvent(1500, 10, addSprite, this, 'sonic');
2523

2624
}
2725

28-
function addSprite() {
26+
function addSprite(key) {
2927

30-
game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
28+
console.log(arguments);
29+
30+
game.add.sprite(game.world.randomX, game.world.randomY, key);
3131

3232
}
3333

3434
function update() {
3535

36-
37-
3836
}
3937

4038
function render() {
4139

42-
game.debug.renderText(timer.ms, 32, 32);
40+
game.debug.renderText(game.time._timer.ms, 32, 32);
4341
// game.debug.renderCameraInfo(game.camera, 32, 32);
4442

4543
}

src/core/Game.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ Phaser.Game.prototype = {
431431
this.net = new Phaser.Net(this);
432432
this.debug = new Phaser.Utils.Debug(this);
433433

434+
this.time.boot();
434435
this.stage.boot();
435436
this.world.boot();
436437
this.input.boot();

src/system/Canvas.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
/**
8-
* The Canvas class handles everything related to the <canvas> tag as a DOM Element, like styles, offset, aspect ratio
8+
* The Canvas class handles everything related to creating the `canvas` DOM tag that Phaser will use, including styles, offset and aspect ratio.
99
*
1010
* @class Phaser.Canvas
1111
* @static
@@ -56,8 +56,22 @@ Phaser.Canvas = {
5656
var box = element.getBoundingClientRect();
5757
var clientTop = element.clientTop || document.body.clientTop || 0;
5858
var clientLeft = element.clientLeft || document.body.clientLeft || 0;
59-
var scrollTop = window.pageYOffset || element.scrollTop || document.body.scrollTop;
60-
var scrollLeft = window.pageXOffset || element.scrollLeft || document.body.scrollLeft;
59+
60+
// Without this check Chrome is now throwing console warnings about strict vs. quirks :(
61+
62+
var scrollTop = 0;
63+
var scrollLeft = 0;
64+
65+
if (document.compatMode === 'CSS1Compat')
66+
{
67+
scrollTop = window.pageYOffset || document.documentElement.scrollTop || element.scrollTop || 0;
68+
scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || element.scrollLeft || 0;
69+
}
70+
else
71+
{
72+
scrollTop = window.pageYOffset || document.body.scrollTop || element.scrollTop || 0;
73+
scrollLeft = window.pageXOffset || document.body.scrollLeft || element.scrollLeft || 0;
74+
}
6175

6276
point.x = box.left + scrollLeft - clientLeft;
6377
point.y = box.top + scrollTop - clientTop;

src/system/Device.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ Phaser.Device = function () {
150150
*/
151151
this.vibration = false;
152152

153+
/**
154+
* @property {boolean} quirksMode - Is the browser running in strict mode (false) or quirks mode? (true)
155+
* @default
156+
*/
157+
this.quirksMode = false;
158+
153159
// Browser
154160

155161
/**
@@ -414,6 +420,8 @@ Phaser.Device.prototype = {
414420

415421
this.pointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
416422

423+
this.quirksMode = (document.compatMode === 'CSS1Compat') ? false : true;
424+
417425
},
418426

419427
/**

src/time/Time.js

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ Phaser.Time = function (game) {
108108
*/
109109
this.lastTime = 0;
110110

111-
this._timer = new Phaser.Timer(this.game, 1, false);
112-
113-
// Listen for game pause/resume events
114-
this.game.onPause.add(this.gamePaused, this);
115-
this.game.onResume.add(this.gameResumed, this);
111+
/**
112+
* @property {Phaser.Timer} _timer - Internal Phaser.Timer object.
113+
* @private
114+
*/
115+
this._timer = new Phaser.Timer(this.game, false);
116116

117117
/**
118118
* @property {boolean} _justResumed - Internal value used to recover from the game pause state.
@@ -126,29 +126,82 @@ Phaser.Time = function (game) {
126126
*/
127127
this._timers = [];
128128

129+
// Listen for game pause/resume events
130+
this.game.onPause.add(this.gamePaused, this);
131+
this.game.onResume.add(this.gameResumed, this);
132+
129133
};
130134

131135
Phaser.Time.prototype = {
132136

137+
/**
138+
* @method Phaser.Time#boot
139+
*/
140+
boot: function () {
133141

142+
this._timer.start();
143+
144+
},
145+
146+
/**
147+
* Adds a new Event to this Timer. The event will fire after the given amount of 'delay' in milliseconds has passed, once the Timer has started running.
148+
* Call Timer.start() once you have added all of the Events you require for this Timer. The delay is in relation to when the Timer starts, not the time it was added.
149+
* @method Phaser.Time#addEvent
150+
* @param {number} delay - The number of milliseconds that should elapse before the Timer will call the given callback.
151+
* @param {function} callback - The callback that will be called when the Timer event occurs.
152+
* @param {object} callbackContext - The context in which the callback will be called.
153+
* @param {...} arguments - The values to be sent to your callback function when it is called.
154+
*/
155+
addEvent: function (delay, callback, callbackContext) {
156+
157+
this._timer.create(delay, false, 0, callback, callbackContext, Array.prototype.splice.call(arguments, 3));
158+
159+
},
160+
161+
/**
162+
* Adds a new Event to this Timer that will repeat for the given number of iterations.
163+
* The event will fire after the given amount of 'delay' milliseconds has passed once the Timer has started running.
164+
* Call Timer.start() once you have added all of the Events you require for this Timer. The delay is in relation to when the Timer starts, not the time it was added.
165+
* @method Phaser.Time#repeatEvent
166+
* @param {number} delay - The number of milliseconds that should elapse before the Timer will call the given callback.
167+
* @param {number} repeatCount - The number of times to repeat the event.
168+
* @param {function} callback - The callback that will be called when the Timer event occurs.
169+
* @param {object} callbackContext - The context in which the callback will be called.
170+
* @param {...} arguments - The values to be sent to your callback function when it is called.
171+
*/
172+
repeatEvent: function (delay, repeatCount, callback, callbackContext) {
173+
174+
this._timer.create(delay, false, repeatCount, callback, callbackContext, Array.prototype.splice.call(arguments, 4));
175+
176+
},
177+
178+
/**
179+
* Adds a new looped Event to this Timer that will repeat forever or until the Timer is stopped.
180+
* The event will fire after the given amount of 'delay' milliseconds has passed once the Timer has started running.
181+
* Call Timer.start() once you have added all of the Events you require for this Timer. The delay is in relation to when the Timer starts, not the time it was added.
182+
* @method Phaser.Time#loopEvent
183+
* @param {number} delay - The number of milliseconds that should elapse before the Timer will call the given callback.
184+
* @param {function} callback - The callback that will be called when the Timer event occurs.
185+
* @param {object} callbackContext - The context in which the callback will be called.
186+
* @param {...} arguments - The values to be sent to your callback function when it is called.
187+
*/
188+
loopEvent: function (delay, callback, callbackContext) {
189+
190+
this._timer.create(delay, true, 0, callback, callbackContext, Array.prototype.splice.call(arguments, 3));
191+
192+
},
134193

135194
/**
136195
* Creates a new stand-alone Phaser.Timer object.
137196
* @method Phaser.Time#create
138-
* @param {number} [timeUnit=1000] - The number of ms that represent 1 unit of time. For example a timer that ticks every second would have a timeUnit value of 1000.
139197
* @param {boolean} [autoDestroy=true] - A Timer that is set to automatically destroy itself will do so after all of its events have been dispatched (assuming no looping events).
140198
* @return {Phaser.Timer} The Timer object that was created.
141199
*/
142-
create: function (timeUnit, autoDestroy) {
200+
create: function (autoDestroy) {
143201

144202
if (typeof autoDestroy === 'undefined') { autoDestroy = true; }
145203

146-
var timer = new Phaser.Timer(this.game, timeUnit, autoDestroy);
147-
148-
if (typeof delay !== 'undefined')
149-
{
150-
timer.add(delay);
151-
}
204+
var timer = new Phaser.Timer(this.game, autoDestroy);
152205

153206
this._timers.push(timer);
154207

@@ -220,6 +273,9 @@ Phaser.Time.prototype = {
220273
this.pausedTime = this.now - this._pauseStarted;
221274
}
222275

276+
// Our internal timer
277+
this._timer.update(this.now);
278+
223279
var i = 0;
224280
var len = this._timers.length;
225281

0 commit comments

Comments
 (0)