Skip to content

Commit c6c579e

Browse files
committed
Phaser.Timer is now feature complete and fully documented. You can create Phaser.TimerEvents on a Timer and lots of new examples have been provided.
1 parent 35e6117 commit c6c579e

16 files changed

Lines changed: 816 additions & 332 deletions

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ module.exports = function (grunt) {
9999
'src/tween/Easing.js',
100100
'src/time/Time.js',
101101
'src/time/Timer.js',
102+
'src/time/TimerEvent.js',
102103
'src/animation/AnimationManager.js',
103104
'src/animation/Animation.js',
104105
'src/animation/Frame.js',

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Significant API changes:
5959

6060
New features:
6161

62+
* Phaser.Timer is now feature complete and fully documented. You can create Phaser.TimerEvents on a Timer and lots of new examples have been provided.
6263
* Gamepad API support has been added with lots of new examples (thanks Karl Macklin)
6364
* Phaser.Game constructor can now be passed a single object containing all of your game settings + Stage settings. Useful for advanced configurations.
6465
* The width/height given to Phaser.Game can now be percentages, i.e. "100%" will set the width to the maximum window innerWidth.
@@ -102,6 +103,7 @@ New Examples:
102103
* Particles - Rain by Jens Anders Bakke.
103104
* Particles - Snow by Jens Anders Bakke.
104105
* Groups - Nested Groups - showing how to embed one Group into another Group.
106+
* Time - Lots of new examples showing how to use the Phaser.Timer class.
105107

106108

107109
Updates:
@@ -132,7 +134,7 @@ Updates:
132134
* Button.clearFrames method has been added.
133135
* Device.quirksMode is a boolean that informs you if the page is in strict (false) or quirks (true) mode.
134136
* 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.
137+
* The Time class now has its own Phaser.Timer which you can access through game.time.events. See the new Timer examples to show how to use them.
136138

137139

138140
Bug Fixes:

build/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
114114
<script src="$path/src/time/Time.js"></script>
115115
<script src="$path/src/time/Timer.js"></script>
116+
<script src="$path/src/time/TimerEvent.js"></script>
116117
117118
<script src="$path/src/animation/AnimationManager.js"></script>
118119
<script src="$path/src/animation/Animation.js"></script>

examples/_site/examples.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@
350350
"file": "group+transform.js",
351351
"title": "group transform"
352352
},
353+
{
354+
"file": "nested+groups.js",
355+
"title": "nested groups"
356+
},
353357
{
354358
"file": "recyling.js",
355359
"title": "recyling"
@@ -797,6 +801,28 @@
797801
"title": "swap tiles"
798802
}
799803
],
804+
"time": [
805+
{
806+
"file": "basic+looped+event.js",
807+
"title": "basic looped event"
808+
},
809+
{
810+
"file": "basic+repeat+event.js",
811+
"title": "basic repeat event"
812+
},
813+
{
814+
"file": "basic+timed+event.js",
815+
"title": "basic timed event"
816+
},
817+
{
818+
"file": "remove+event.js",
819+
"title": "remove event"
820+
},
821+
{
822+
"file": "timed+slideshow.js",
823+
"title": "timed slideshow"
824+
}
825+
],
800826
"tweens": [
801827
{
802828
"file": "bounce.js",

examples/_site/view_full.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118

119119
<script src="../src/time/Time.js"></script>
120120
<script src="../src/time/Timer.js"></script>
121+
<script src="../src/time/TimerEvent.js"></script>
121122

122123
<script src="../src/animation/AnimationManager.js"></script>
123124
<script src="../src/animation/Animation.js"></script>

examples/_site/view_lite.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118

119119
<script src="../src/time/Time.js"></script>
120120
<script src="../src/time/Timer.js"></script>
121+
<script src="../src/time/TimerEvent.js"></script>
121122

122123
<script src="../src/animation/AnimationManager.js"></script>
123124
<script src="../src/animation/Animation.js"></script>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });
3+
4+
function preload() {
5+
6+
game.load.image('ball', 'assets/sprites/pangball.png');
7+
8+
}
9+
10+
var counter = 0;
11+
var text = 0;
12+
13+
function create() {
14+
15+
game.stage.backgroundColor = '#6688ee';
16+
17+
text = game.add.text(game.world.centerX, game.world.centerY, 'Counter: 0', { font: "64px Arial", fill: "#ffffff", align: "center" });
18+
text.anchor.setTo(0.5, 0.5);
19+
20+
// Here we'll create a basic looped event.
21+
// A looped event is like a repeat event but with no limit, it will literally repeat itself forever, or until
22+
23+
// The first parameter is how long to wait before the event fires. In this case 1 second (you could pass in 1000 as the value as well.)
24+
// The next two parameters are the function to call ('updateCounter') and the context under which that will happen.
25+
26+
game.time.events.loop(Phaser.Timer.SECOND, updateCounter, this);
27+
28+
}
29+
30+
function updateCounter() {
31+
32+
counter++;
33+
34+
text.content = 'Counter: ' + counter;
35+
36+
}
37+
38+
function render() {
39+
40+
game.debug.renderText("Time until event: " + game.time.events.duration.toFixed(0), 32, 32);
41+
game.debug.renderText("Next tick: " + game.time.events.next.toFixed(0), 32, 64);
42+
43+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });
3+
4+
function preload() {
5+
6+
game.load.image('ball', 'assets/sprites/pangball.png');
7+
8+
}
9+
10+
function create() {
11+
12+
game.stage.backgroundColor = '#6688ee';
13+
14+
// Here we'll create a basic repeating event.
15+
16+
// The way a repeating event works is that it is placed into the queue once,
17+
// and when it runs its 'repeatCounter' is reduced by 1 and it's moved back into the queue again.
18+
// To this end the queue will only ever have 1 event actually in it.
19+
20+
// The first parameter is how long to wait before the event fires. In this case 2 seconds (you could pass in 2000 as the value as well.)
21+
// The second parameter is how many times the event will run in total. Here we'll run it 10 times.
22+
// The next two parameters are the function to call ('createBall') and the context under which that will happen.
23+
24+
// Once the event has been called 10 times it will never be called again.
25+
26+
game.time.events.repeat(Phaser.Timer.SECOND * 2, 10, createBall, this);
27+
28+
}
29+
30+
function createBall() {
31+
32+
// A bouncey ball sprite just to visually see what's going on.
33+
34+
var ball = game.add.sprite(game.world.randomX, 0, 'ball');
35+
36+
ball.body.gravity.y = 200;
37+
ball.body.bounce.y = 0.5;
38+
ball.body.collideWorldBounds = true;
39+
40+
}
41+
42+
function render() {
43+
44+
game.debug.renderText("Time until event: " + game.time.events.duration.toFixed(0), 32, 32);
45+
game.debug.renderText("Next tick: " + game.time.events.next.toFixed(0), 32, 64);
46+
47+
}

examples/time/basic timed event.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });
3+
4+
function preload() {
5+
6+
game.load.image('bisley', 'assets/pics/alex-bisleys_horsy_5.png');
7+
8+
}
9+
10+
var picture;
11+
12+
function create() {
13+
14+
game.stage.backgroundColor = '#6688ee';
15+
16+
picture = game.add.sprite(game.world.centerX, game.world.centerY, 'bisley');
17+
picture.anchor.setTo(0.5, 0.5);
18+
19+
// Here we'll create a basic timed event. This is a one-off event, it won't repeat or loop:
20+
// The first parameter is how long to wait before the event fires. In this case 4 seconds (you could pass in 4000 as the value as well.)
21+
// The next parameter is the function to call ('fadePicture') and finally the context under which that will happen.
22+
23+
game.time.events.add(Phaser.Timer.SECOND * 4, fadePicture, this);
24+
25+
}
26+
27+
function fadePicture() {
28+
29+
game.add.tween(picture).to( { alpha: 0 }, 2000, Phaser.Easing.Linear.None, true);
30+
31+
}
32+
33+
function render() {
34+
35+
game.debug.renderText("Time until event: " + game.time.events.duration, 32, 32);
36+
37+
}

examples/time/remove event.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create, render: render });
3+
4+
var counters = [];
5+
var text = [];
6+
var timerEvents = [];
7+
var i = 9;
8+
9+
function create() {
10+
11+
game.stage.backgroundColor = '#6688ee';
12+
13+
for (var i = 0; i < 10; i++)
14+
{
15+
counters[i] = 0;
16+
text[i] = game.add.text(game.world.centerX, 80 + (40 * i), 'Counter ' + i + ' = 0', { font: "32px Arial", fill: "#ffffff", align: "center" });
17+
text[i].anchor.setTo(0.5, 0);
18+
19+
// Here we create our timer events. They will be set to loop at a random value between 250ms and 1000ms
20+
timerEvents[i] = game.time.events.loop(game.rnd.integerInRange(250, 1000), updateCounter, this, i);
21+
}
22+
23+
// Click to remove
24+
game.input.onDown.add(removeCounter, this);
25+
26+
}
27+
28+
function updateCounter(idx) {
29+
30+
counters[idx]++;
31+
32+
text[idx].content = 'Counter ' + idx + ' = ' + counters[idx];
33+
34+
}
35+
36+
function removeCounter() {
37+
38+
if (i >= 0)
39+
{
40+
// Removes the timer, starting with the top one and working down
41+
game.time.events.remove(timerEvents[i]);
42+
43+
// Just updates the text
44+
text[i].style.fill = '#3344aa';
45+
text[i].content = 'Counter ' + i + ' removed';
46+
i--;
47+
}
48+
49+
}
50+
51+
function render() {
52+
53+
game.debug.renderText("Queued events: " + game.time.events.length + ' - click to remove', 32, 32);
54+
55+
}

0 commit comments

Comments
 (0)