Phaser 2.0.5 test environment

Basic Phaser setup for testing

by Lewis Lane

HTML

<script src="http://examples.phaser.io/_site/js/phaser.js"></script>
<div id="test"></div>

JavaScript

// Phaser 2.0.5 test environment
// Camera following a group

var game = new Phaser.Game(500, 400, Phaser.AUTO, 'test');

var BasicGame = function(game) {};

BasicGame.Boot = function (game) {
    // nothing here
};

BasicGame.Boot.prototype = 
{
    preload: function() {
      game.time.advancedTiming = true;
    },
	create: function() 
	{
        // generate a background so we can see the world/camera movement
        var checkerboard = game.add.bitmapData(64, 64);
        checkerboard.ctx.fillStyle = 'rgb(80,80,80)';
        checkerboard.ctx.fillRect(0, 0, 32, 32);
        checkerboard.ctx.fillRect(32, 32, 32, 32);
        
        var bg = game.add.tileSprite(-512, -512, 1024, 1024, checkerboard);
        
        this.square = game.add.group();
        this.square.speed = 5;
        
        var squareGraphics = game.add.graphics();
        squareGraphics.beginFill(0xffffff);
        squareGraphics.drawCircle(0, 0, 16);
        this.square.add(squareGraphics);
        
        this.cursors = game.input.keyboard.createCursorKeys();
        this.game.world.setBounds(-512, -512, 1024, 1024);
        this.game.camera.follow(this.square);
    },
    update: function() 
    {
        if (this.cursors.left.isDown) {
            this.square.x -= this.square.speed;
        }
        else if (this.cursors.right.isDown) {
            this.square.x += this.square.speed;   
        }
        if (this.cursors.up.isDown) {
            this.square.y -= this.square.speed;
        }
        else if (this.cursors.down.isDown) {
            this.square.y += this.square.speed;   
        }
    }, 
    render: function()
    {
        game.debug.text(game.time.fps || '--', 2, 14, "#00ff00");   
    }
};

game.state.add('Boot', BasicGame.Boot);
game.state.start('Boot');