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
// Cheaty eye tracking with non-circular bounds

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

var BasicGame = function(game) {};

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

var pupilRadius = 25, eyeRadius = 60, eyePadding = 5, eye;

BasicGame.Boot.prototype = 
{
    preload: function() {
      game.time.advancedTiming = true;
        game.stage.backgroundColor = "#fcc";
    },
	create: function() 
	{
      // Add the eyeball
      eye = game.add.graphics();
      eye.position.setTo(game.world.centerX, game.world.centerY);
      eye.beginFill(0xffffff);
      eye.lineStyle(3, 0x000000);
      eye.drawCircle(0, 0, eyeRadius + pupilRadius + eyePadding);
      
      // Add the pupil
      eye.pupil = game.add.graphics();
      eye.pupil.beginFill(0x000000)
      eye.pupil.drawCircle(0, 0, pupilRadius)
      
      // Add a little shine on the pupil ;)
      eye.pupil.beginFill(0xffffff, 0.9)
      eye.pupil.drawCircle(pupilRadius * -0.5, pupilRadius * -0.5, pupilRadius * 0.33)
      
      // Add the pupil to the eye
      eye.addChild(eye.pupil);
        
      // Reduce the x scale by a quarter to give an oval shape
      eye.scale.x = 0.75
      
      // Reverse the scale on the pupil so it's circular again
      eye.pupil.scale.x = 1.25;
      
      // Tilt the eye 20 degrees to the left
      eye.angle = -20;
    },
    update: function() {
        this.updatePupil();
    },
    render: function()
    {
        game.debug.text(game.time.fps || '--', 2, 14, "#00ff00");   
    },
    updatePupil: function() {
      // Our standard circular eye tracking code
      var dx = eye.x - game.input.activePointer.position.x;
      var dy = eye.y - game.input.activePointer.position.y;
      var r = Math.sqrt(dx * dx + dy * dy);
      var x = (r < eyeRadius) ? dx : dx * eyeRadius / r;
      var y = (r < eyeRadius) ? dy : dy * eyeRadius / r;
      eye.pupil.position.setTo(x * -1, y * -1);   
   ...