Skip to content

Commit 3d47839

Browse files
committed
StateManager improving in leaps and bounds.
1 parent 4bbcc38 commit 3d47839

9 files changed

Lines changed: 776 additions & 232 deletions

File tree

examples/flow.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
// Your game will work either from within a closure or not, it doesn't matter
1414
(function () {
1515

16-
// These are all optional parameters now
17-
var game = new Phaser.Game();
16+
var game = new Phaser.Game(800, 600, Phaser.RENDERER_AUTO, '', { preload: preload, create: create });
1817

1918
// In this approach we're simply using functions in the current scope to do what we need
20-
game.launch(this, preload, create);
19+
// game.launch(this, preload, create);
2120

2221
function preload() {
2322

@@ -45,7 +44,7 @@ function create() {
4544

4645
}
4746

48-
/})();
47+
})();
4948

5049
</script>
5150

examples/flow2.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
state.preload = function() {
1818

19-
console.log('state preload called');
19+
console.log('state preload called', this);
2020

2121
// When an instance of Phaser.State is added to Phaser.Game it becomes bound to that game
2222
// which opens up lots of handy short-cuts to helpers and libs, such as this.math, this.load, this.cache, etc
@@ -32,7 +32,7 @@
3232
console.log('state create called');
3333

3434
// Let's try adding an image to the DOM
35-
document.body.appendChild(this.cache.getImage('cockpit'));
35+
document.body.appendChild(this.cache.getImage('overdose'));
3636

3737
// And some text
3838
var para = document.createElement('pre');
@@ -41,11 +41,9 @@
4141

4242
}
4343

44-
var megaBlasterGame = new Phaser.Game();
45-
4644
// In this approach we're using a Phaser.State object and switching to it
4745
// This gives us the advantage that from within state functions you can access 'this' and most the properties of Phaser.Game
48-
megaBlasterGame.switchState(state);
46+
var game = new Phaser.Game(800, 600, Phaser.RENDERER_AUTO, '', state);
4947

5048
})();
5149

examples/flow3.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@
3737
}
3838
}
3939

40-
var game = new Phaser.Game();
41-
42-
// This method now works either from within a closure or not
43-
game.switchState(state);
40+
// In this example we're passing in a custom state object, not an instance of Phaser.State
41+
var game = new Phaser.Game(800, 600, Phaser.RENDERER_AUTO, '', state);
4442

4543
})();
4644

examples/flow4.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
<script type="text/javascript">
12+
13+
var MegaGame = {};
14+
15+
MegaGame.MainMenu = function (game) {
16+
// A reference to the local game will always be passed to your state when created
17+
// You should store it somewhere handy for use in your functions, like this ...
18+
this.game = game;
19+
};
20+
21+
MegaGame.MainMenu.prototype = {
22+
23+
preload: function () {
24+
25+
console.log('MainMenu.preload');
26+
this.game.load.image('bladerunner', 'assets/pics/acryl_bladerunner.png');
27+
28+
},
29+
30+
create: function () {
31+
32+
console.log('MainMenu.create');
33+
document.body.appendChild(this.game.cache.getImage('bladerunner'));
34+
35+
}
36+
37+
};
38+
39+
(function () {
40+
41+
// Finally in this example we pass in a custom State object which will be created upon boot
42+
var game = new Phaser.Game(800, 600, Phaser.RENDERER_AUTO, '', MegaGame.MainMenu);
43+
44+
})();
45+
46+
</script>
47+
48+
</body>
49+
</html>

examples/flow5.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var state = {
16+
17+
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+
23+
},
24+
25+
create: function() {
26+
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'));
31+
32+
// And some text
33+
var para = document.createElement('pre');
34+
para.innerHTML = game.cache.getText('copyright');
35+
document.body.appendChild(para);
36+
37+
}
38+
}
39+
40+
var game = new Phaser.Game(800, 600, Phaser.RENDERER_AUTO, '');
41+
42+
// In this instance we'll change to the state ourselves rather than pass it in the game constructor
43+
44+
45+
})();
46+
47+
</script>
48+
49+
</body>
50+
</html>

examples/js.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<script src="../src/pixi/utils/Polyk.js"></script>
4343
<script src="../src/pixi/utils/Utils.js"></script>
4444

45+
<script src="../src/core/StateManager.js"></script>
4546
<script src="../src/core/State.js"></script>
4647
<script src="../src/system/RequestAnimationFrame.js"></script>
4748
<script src="../src/system/Device.js"></script>

examples/test.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
</head>
6+
<body>
7+
8+
<script type="text/javascript">
9+
10+
var BigGame = function(context) {
11+
12+
console.log(context);
13+
console.log(context['hello']);
14+
// this.pendingCallback = callback;
15+
this.pendingCallback = context['hello'];
16+
17+
var _this = this;
18+
19+
this._onLoop = function () {
20+
return _this.boot();
21+
}
22+
23+
if (document.readyState === 'complete' || document.readyState === 'interactive')
24+
{
25+
window.setTimeout(this._onLoop, 0);
26+
}
27+
else
28+
{
29+
document.addEventListener('DOMContentLoaded', this._onLoop, false);
30+
window.addEventListener('load', this._onLoop, false);
31+
}
32+
33+
};
34+
35+
BigGame.prototype = {
36+
37+
isBooted: false,
38+
pendingCallback: null,
39+
_onLoop: null,
40+
41+
boot: function () {
42+
43+
if (this.isBooted) {
44+
return;
45+
}
46+
else
47+
{
48+
document.removeEventListener('DOMContentLoaded', this._onLoop);
49+
window.removeEventListener('load', this._onLoop);
50+
51+
this.pendingCallback();
52+
}
53+
54+
},
55+
56+
check: function () {
57+
console.log(Math.random());
58+
}
59+
60+
};
61+
62+
(function () {
63+
64+
// because this is in a closure we have to pass a reference to the function directly, it can't guess it
65+
// var bob = new BigGame(hello);
66+
//var bob = new BigGame(this);
67+
68+
function hello() {
69+
console.log('hello world 2');
70+
console.log(bob);
71+
bob.check();
72+
}
73+
74+
})();
75+
76+
</script>
77+
78+
</body>
79+
</html>

0 commit comments

Comments
 (0)