Phaser current version test environment
Basic Phaser setup for testing
by Lewis Lane
HTML
<script src="https://rawgit.com/photonstorm/phaser/master/build/phaser.min.js"></script>
<div id="test"></div>
JavaScript
// Phaser current version test environment
// Code structuring and accessing values across states
var BasicGame = {
// Initialise Phaser
game: new Phaser.Game(500, 400, Phaser.AUTO, 'test'),
// Set our score as a property on the BasicGame object which encompasses our game code
score: 0
};
// The Main state is a property of BasicGame too - this keeps everything nicely in one place
BasicGame.Main = {
create: function (game) {
// Increment the score to a random number over 5 seconds, then go to the next state - note that we
// tween BasicGame's score property, and that's where we can access it - BasicGame.score
this.game.add.tween(BasicGame).to({
score: game.rnd.integerInRange(1000, 10000)
}, 5000, null, true).onComplete.add(function () {
this.game.state.start("End");
}, this);
},
render: function (game) {
game.debug.text("State: " + game.state.current, 2, 14, "#ffffff");
game.debug.text("Score: " + Math.round(BasicGame.score), 2, 34, "#ffff00");
}
};
// Again, the End state is another property of BasicGame
BasicGame.End = {
render: function (game) {
game.debug.text("State: " + game.state.current, 2, 14, "#ffffff");
game.debug.text("Well done, your score was: " + BasicGame.score, 2, 34, "#00ff00");
}
};
// Add our states
BasicGame.game.state.add('Main', BasicGame.Main);
BasicGame.game.state.add('End', BasicGame.End);
// Start the 'Main' state
BasicGame.game.state.start('Main');