Phaser current version test environment
Basic Phaser setup for testing
by shohan4556
HTML
<script src="https://rawgit.com/photonstorm/phaser/master/build/phaser.min.js"></script>
<div id="test"></div>
JavaScript
// Phaser current version test environment
// Collision increases points by one
var game = new Phaser.Game(500, 400, Phaser.AUTO, 'test');
var BasicGame = function(game) {};
BasicGame.Boot = function (game) {
// nothing here
};
var score = 0, player, leftwall, rightwall;
BasicGame.Boot.prototype =
{
preload: function() {
game.time.advancedTiming = true;
},
create: function()
{
// Create a player graphic
var playerbmp = game.add.bitmapData(48, 32);
playerbmp.ctx.fillStyle = "#0f0";
playerbmp.ctx.rect(0, 0, 32, 32);
playerbmp.ctx.rect(32, 8, 16, 16);
playerbmp.ctx.fill();
// Add graphic to a sprite
player = game.add.sprite(game.world.centerX, game.world.centerY, playerbmp);
// Center the texture
player.anchor.set(0.5);
// Create wall graphics
var wallbmp = game.add.bitmapData(16, 400);
wallbmp.ctx.fillStyle = "#ff0";
wallbmp.ctx.rect(0, 0, 16, 400);
wallbmp.ctx.fill();
// Add our two wall sprites
leftwall = game.add.sprite(0, 0, wallbmp);
rightwall = game.add.sprite(game.world.width - wallbmp.width, 0, wallbmp);
// Enable physics on the player and walls
game.physics.arcade.enable([player, leftwall, rightwall]);
// Ensure the walls don't move when hit
leftwall.body.immovable = true;
rightwall.body.immovable = true;
// Stop the player going off the edge of the screen
player.body.collideWorldBounds = true;
// Make sure the player bounces perfectly on the x axis
player.body.bounce.x = 1;
// Add some gravity
player.body.gravity.setTo(0, 500);
// Make it so clicking/tapping causes the player to jump/flap
game.input.onDown.add(function() {
player.body.velocity.y = -300;
});
//...