Skip to content

Commit 16749ed

Browse files
committed
More state swapping features and optimised Game.loop.
1 parent 3d47839 commit 16749ed

4 files changed

Lines changed: 194 additions & 300 deletions

File tree

examples/flow5.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,42 @@
1212

1313
(function () {
1414

15-
var state = {
15+
var mainMenu = {
1616

1717
preload: function() {
18-
19-
console.log('state preload called');
20-
game.load.image('rememberMe', 'assets/pics/remember-me.jpg');
21-
game.load.text('copyright', 'assets/warning - copyright.txt');
22-
18+
console.log('mainMenu preload');
19+
game.load.image('nocooper', 'assets/pics/1984-nocooper-space.png');
2320
},
2421

2522
create: function() {
23+
console.log('mainMenu create');
24+
document.body.appendChild(game.cache.getImage('nocooper'));
25+
}
26+
}
2627

27-
console.log('state create called');
28-
29-
// Let's try adding an image to the DOM
30-
document.body.appendChild(game.cache.getImage('rememberMe'));
28+
var levelSelect = {
3129

32-
// And some text
33-
var para = document.createElement('pre');
34-
para.innerHTML = game.cache.getText('copyright');
35-
document.body.appendChild(para);
30+
preload: function() {
31+
console.log('levelSelect preload');
32+
game.load.image('touhou', 'assets/pics/aya_touhou_teng_soldier.png');
33+
},
3634

35+
create: function() {
36+
console.log('levelSelect create');
37+
document.body.appendChild(game.cache.getImage('touhou'));
3738
}
3839
}
3940

40-
var game = new Phaser.Game(800, 600, Phaser.RENDERER_AUTO, '');
41+
// No parameters given, which means no default state is created or started
42+
var game = new Phaser.Game();
4143

42-
// In this instance we'll change to the state ourselves rather than pass it in the game constructor
44+
// In this example we've created 2 states above (mainMenu and levelSelect)
45+
// We'll add them both to the game
46+
game.state.add('menu', mainMenu);
47+
game.state.add('select', levelSelect);
4348

49+
// Now we can either start the state we want directly, or we could have passed 'true' as the 3rd parameter in state.add
50+
game.state.start('select');
4451

4552
})();
4653

examples/flow6.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<input type="button" id="menu" value="Main Menu" />
12+
<input type="button" id="select" value="Level Select" />
13+
14+
<script type="text/javascript">
15+
16+
(function () {
17+
18+
var preloader = {
19+
20+
preload: function() {
21+
console.log('preloader.preload');
22+
game.load.image('nocooper', 'assets/pics/1984-nocooper-space.png');
23+
game.load.image('touhou', 'assets/pics/aya_touhou_teng_soldier.png');
24+
}
25+
26+
}
27+
28+
var mainMenu = {
29+
30+
create: function() {
31+
console.log('mainMenu create');
32+
document.body.appendChild(game.cache.getImage('nocooper'));
33+
}
34+
}
35+
36+
var levelSelect = {
37+
38+
create: function() {
39+
console.log('levelSelect create');
40+
document.body.appendChild(game.cache.getImage('touhou'));
41+
}
42+
}
43+
44+
// No parameters given, which means no default state is created or started
45+
var game = new Phaser.Game();
46+
47+
game.state.add('preloader', preloader, true);
48+
game.state.add('menu', mainMenu);
49+
game.state.add('select', levelSelect);
50+
51+
var b1 = document.getElementById('menu').onclick = function() { game.state.start('menu') };
52+
var b2 = document.getElementById('select').onclick = function() { game.state.start('select') };
53+
54+
})();
55+
56+
</script>
57+
58+
</body>
59+
</html>

src/Game.js

Lines changed: 16 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Phaser.Game = function (width, height, renderer, parent, state) {
3030
if (typeof height === "undefined") { height = 600; }
3131
if (typeof renderer === "undefined") { renderer = Phaser.RENDERER_AUTO; }
3232
if (typeof parent === "undefined") { parent = ''; }
33-
if (typeof state === "undefined") { state = {}; }
33+
if (typeof state === "undefined") { state = null; }
3434

3535
this.id = Phaser.GAMES.push(this) - 1;
3636
this.parent = parent;
@@ -43,9 +43,7 @@ Phaser.Game = function (width, height, renderer, parent, state) {
4343

4444
console.log('Phaser.Game', width, height, renderer, parent);
4545

46-
// Pass 'state' to the StateManager?
4746
this.state = new Phaser.StateManager(this, state);
48-
// this._tempState = state;
4947

5048
var _this = this;
5149

@@ -237,12 +235,8 @@ Phaser.Game.prototype = {
237235

238236
console.log('Phaser.Game boot');
239237

240-
// Probably not needed any more
241-
// var _this = this;
242-
243238
if (!document.body) {
244239
window.setTimeout(this._onBoot, 20);
245-
// setTimeout(Phaser.GAMES[_this.id].boot(parent, width, height), 13);
246240
}
247241
else
248242
{
@@ -252,6 +246,8 @@ Phaser.Game.prototype = {
252246
this.onPause = new Phaser.Signal();
253247
this.onResume = new Phaser.Signal();
254248

249+
this.isBooted = true;
250+
255251
this.device = new Phaser.Device();
256252
this.net = new Phaser.Net(this);
257253
this.math = Phaser.Math;
@@ -267,7 +263,6 @@ Phaser.Game.prototype = {
267263
this.rnd = new Phaser.RandomDataGenerator([(Date.now() * Math.random()).toString()]);
268264
// this.physics = new Phaser.Physics.PhysicsManager(this);
269265
this.plugins = new Phaser.PluginManager(this, this);
270-
// this.state = new Phaser.StateManager(this, this._tempState);
271266

272267
this.load.onLoadComplete.add(this.loadComplete, this);
273268

@@ -278,68 +273,16 @@ Phaser.Game.prototype = {
278273

279274
console.log('Phaser', Phaser.VERSION, 'initialized');
280275

281-
this.isBooted = true;
282276
this.isRunning = true;
283277
this._loadComplete = false;
284278

285-
// this.raf = new Phaser.RequestAnimationFrame(this);
286-
// this.raf.start();
287-
288-
// boot sm
279+
this.raf = new Phaser.RequestAnimationFrame(this);
280+
this.raf.start();
289281

290282
}
291283

292284
},
293285

294-
/**
295-
* Launch the game
296-
* @param callbackContext Which context will the callbacks be called with.
297-
* @param preloadCallback {function} Preload callback invoked when init default screen.
298-
* @param createCallback {function} Create callback invoked when create default screen.
299-
* @param updateCallback {function} Update callback invoked when update default screen.
300-
* @param renderCallback {function} Render callback invoked when render default screen.
301-
*/
302-
launch: function (context, preload, create, update, render) {
303-
304-
/*
305-
this.callbackContext = context;
306-
307-
this.onPreloadCallback = preload || null;
308-
this.onCreateCallback = create || null;
309-
this.onUpdateCallback = update || null;
310-
this.onRenderCallback = render || null;
311-
312-
if (this.onPreloadCallback == null && this.onCreateCallback == null && this.onUpdateCallback == null && this.onRenderCallback == null && this._pendingState == null)
313-
{
314-
console.warn("Phaser cannot start: No preload, create, update or render functions given and no pending State found");
315-
}
316-
else
317-
{
318-
if (this.isBooted)
319-
{
320-
console.log('launch has set the callbacks and dom is booted so lets rock');
321-
322-
this.startState();
323-
324-
// if (this._pendingState)
325-
// {
326-
// this.switchState(this._pendingState, false, false);
327-
// }
328-
// else
329-
// {
330-
// this.startState();
331-
// }
332-
333-
}
334-
else
335-
{
336-
console.log('launch has set the callbacks but cant start because the DOM isnt booted yet');
337-
}
338-
}
339-
*/
340-
341-
},
342-
343286
/**
344287
* Called when the load has finished, after preload was run.
345288
*/
@@ -351,12 +294,8 @@ Phaser.Game.prototype = {
351294

352295
this.state.loadComplete();
353296

354-
this.load.onLoadComplete.remove(this.loadComplete, this);
355-
356-
// if (this.onCreateCallback) {
357-
// this.onCreateCallback.call(this.callbackContext);
358-
// // this.onCreateCallback.call(this);
359-
// }
297+
// ?
298+
// this.load.onLoadComplete.remove(this.loadComplete, this);
360299

361300
},
362301

@@ -377,49 +316,33 @@ Phaser.Game.prototype = {
377316

378317
if (this._loadComplete)
379318
{
380-
if (this.onUpdateCallback)
381-
{
382-
this.onUpdateCallback.call(this.callbackContext);
383-
}
319+
this.state.update();
384320

385321
// this.world.postUpdate();
386322
this.plugins.postUpdate();
387323
this.plugins.preRender();
388324

389-
if (this.onPreRenderCallback)
390-
{
391-
this.onPreRenderCallback.call(this.callbackContext);
392-
}
325+
this.state.preRender();
393326

394327
// this.renderer.render();
395328
this.plugins.render();
396329

397-
if (this.onRenderCallback)
398-
{
399-
this.onRenderCallback.call(this.callbackContext);
400-
}
330+
this.state.render();
401331

402332
this.plugins.postRender();
403333
}
404334
else
405335
{
406336
// Still loading assets
407-
if (this.onLoadUpdateCallback)
408-
{
409-
this.onLoadUpdateCallback.call(this.callbackContext);
410-
}
337+
this.state.loadUpdate();
411338

412339
// this.world.postUpdate();
413340
this.plugins.postUpdate();
341+
414342
this.plugins.preRender();
415343
// this.renderer.render();
416344
this.plugins.render();
417-
418-
if (this.onLoadRenderCallback)
419-
{
420-
this.onLoadRenderCallback.call(this.callbackContext);
421-
}
422-
345+
this.state.loadRender();
423346
this.plugins.postRender();
424347
}
425348

@@ -430,15 +353,9 @@ Phaser.Game.prototype = {
430353
*/
431354
destroy: function () {
432355

433-
this.callbackContext = null;
434-
this.onPreloadCallback = null;
435-
this.onLoadRenderCallback = null;
436-
this.onLoadUpdateCallback = null;
437-
this.onCreateCallback = null;
438-
this.onUpdateCallback = null;
439-
this.onRenderCallback = null;
440-
this.onPausedCallback = null;
441-
this.onDestroyCallback = null;
356+
this.state.destroy();
357+
358+
this.state = null;
442359
this.cache = null;
443360
this.input = null;
444361
this.load = null;

0 commit comments

Comments
 (0)