Phaser current version test environment

Basic Phaser setup for testing

by Lewis Lane

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/phaser/2.1.0/phaser.min.js"></script>
<div id="test"></div>

CSS

body {
    background: #333;
}

JavaScript

// Phaser current version test environment
// Angle to cardinal directions

var game = new Phaser.Game(600, 400, Phaser.AUTO, 'test', null, false, false);

var BasicGame = function (game) {};

BasicGame.Boot = function (game) {};

var directions = ["W", "NW", "N", "NE", "E", "SE", "S", "SW"], direction, circle;

BasicGame.Boot.prototype = {
    preload: function () {

    },
    create: function () {
        var circleBmd = game.add.bitmapData(32, 32);
        circleBmd.circle(16, 16, 16, '#0f0');
        circle = game.add.sprite(game.world.centerX, game.world.centerY, circleBmd);
        circle.anchor.set(0.5);
        game.physics.arcade.enable(circle);
    },
    update: function () {
        if (game.input.activePointer.isDown) {
            var angle = game.physics.arcade.moveToPointer(circle, 20);
            direction = this.getCardinal(angle, true);
        }
        else {
            circle.body.velocity.set(0);
            direction = null;
        }
    },
    render: function () {
        game.debug.text(direction || "--", 2, 14, '#0f0');
    },
    getCardinal: function(angle, diagonals) {
        if (diagonals) {
            angle = Math.round((angle + Math.PI) / (Math.PI * 2) * 8) % 8;
            return (directions[angle]);
        }
        else {
            angle = Math.round((angle + Math.PI) / (Math.PI * 2) * 4) % 4;
            return (directions[angle * 2]);            
        }
    }
};

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