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
// 'Arc' cheat tweening

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() 
	{
               
        var ball = game.add.graphics();
        ball.beginFill(0xffffff);
        ball.drawCircle(20, 20, 20);
        
        var target = game.add.graphics();
        target.lineStyle(2, 0xffffff, 0.5);
        target.drawCircle(20, 20, 20);
        target.x = 200;
        target.y = 10;
        
        // set the initial y position of the ball
        ball.x = 50
        ball.y = 200;   

        // throw the ball 'through' the target position
        this.throwThroughXY(ball, target.x, target.y, 2000);
    },
    update: function() 
    {
        if (this.game.input.keyboard.justPressed(Phaser.Keyboard.SPACEBAR)) {
          game.tweens.remove(this.idleBallTween);   
        }
    }, 
    render: function()
    {
        game.debug.text(game.time.fps || '--', 2, 14, "#00ff00");   
    },
    throwThroughXY: function(obj, x, y, time) {
        var target = new Phaser.Point(x, y);
        
        // tween the object from left to right in a linear fashion
        game.add.tween(obj).to({x: target.x + (target.x - obj.x)  }, time, null, true);
        
        // store the original y value
        var originalY = obj.y;
        
        // chain tween the object up to the Y value with a sinusoidal ease out, then back down with an ease in
        game.add.tween(obj)
          .to({y: target.y}, time * 0.5, Phaser.Easing.Sinusoidal.Out)
          .to({y: originalY}, time * 0.5, Phaser.Easing.Sinusoidal.In)
          .start();
    }
};

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