Gamedev Phaser Workshop - lesson 15
Buttons
by Andrzej Mazur
HTML
<script src="https://end3r.github.io/Gamedev-Phaser-Content-Kit/demos/js/phaser.2.4.2.min.js"></script>
CSS
* { padding: 0; margin: 0; }
JavaScript
var game = new Phaser.Game(480, 320, Phaser.AUTO, null, {preload: preload, create: create, update: update});
var ball;
var paddle;
var bricks;
var newBrick;
var brickInfo;
var scoreText;
var score = 0;
var lives = 3;
var livesText;
var lifeLostText;
var playing = false;
var startButton;
function preload() {
handleRemoteImagesOnJSFiddle();
game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
game.scale.pageAlignHorizontally = true;
game.scale.pageAlignVertically = true;
game.stage.backgroundColor = '#eee';
game.load.image('paddle', 'img/paddle.png');
game.load.image('brick', 'img/brick.png');
game.load.spritesheet('ball', 'img/wobble.png', 20, 20);
game.load.spritesheet('button', 'img/button.png', 120, 40);
}
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
game.physics.arcade.checkCollision.down = false;
ball = game.add.sprite(game.world.width*0.5, game.world.height-25, 'ball');
ball.animations.add('wobble', [0,1,0,2,0,1,0,2,0], 24);
ball.anchor.set(0.5);
game.physics.enable(ball, Phaser.Physics.ARCADE);
ball.body.collideWorldBounds = true;
ball.body.bounce.set(1);
ball.checkWorldBounds = true;
ball.events.onOutOfBounds.add(ballLeaveScreen, this);
paddle = game.add.sprite(game.world.width*0.5, game.world.height-5, 'paddle');
paddle.anchor.set(0.5,1);
game.physics.enable(paddle, Phaser.Physics.ARCADE);
paddle.body.immovable = true;
initBricks();
textStyle = { font: '18px Arial', fill: '#0095DD' };
scoreText = game.add.text(5, 5, 'Points: 0', textStyle);
livesText = game.add.text(game.world.width-5, 5, 'Lives: '+lives, textStyle);
livesText.anchor.set(1,0);
lifeLostText = game.add.text(game.world.width*0.5, game.world.height*0.5, 'Life lost, tap to continue', textStyle);
lifeLostText.anchor.set(0.5);
lifeLostText.visible = false;
startButton = game.add.button(game.world.width*0.5, game.world.height*0.5, 'button',...