• Skip to main content
  • Select language
  • Skip to search
MDN Web Docs
  • Technologies
    • HTML
    • CSS
    • JavaScript
    • Graphics
    • HTTP
    • APIs / DOM
    • WebExtensions
    • MathML
  • References & Guides
    • Learn web development
    • Tutorials
    • References
    • Developer Guides
    • Accessibility
    • Game development
    • ...more docs
Game development
  1. MDN
  2. Game development
  3. Tutorials
  4. 2D breakout game using Phaser
  5. Buttons

Buttons

In This Article
  1. New variables
  2. Loading the button spritesheet
  3. Adding the button to the game
  4. Keeping the paddle still before the game starts
  5. Compare your code
  6. Next steps

« PreviousNext »

This is the 15th step out of 16 of the Gamedev Phaser tutorial. You can find the source code as it should look after completing this lesson at Gamedev-Phaser-Content-Kit/demos/lesson15.html.

Instead of starting the game right away we can leave that decision to the player by adding a Start button they can press. Let's investigate how to do that.

New variables

We will need a variable to store a boolean value representing whether the game is currently being played or not, and another one to represent our button. Add these lines below your other variable definitions:

var playing = false;
var startButton;

Loading the button spritesheet

We can load the button spritesheet the same way we loaded the ball's wobble animation. Add the following to the bottom of the preload() function:

game.load.spritesheet('button', 'img/button.png', 120, 40);

A single button frame is 120 pixels wide and 40 pixels high.

You also need to grab the button spritesheet from Github, and save it in your /img directory.

Adding the button to the game

Adding the new button to the game is done by using the add.button method. Add the following lines to the bottom of your create() function:

startButton = game.add.button(game.world.width*0.5, game.world.height*0.5, 'button', startGame, this, 1, 0, 2);
startButton.anchor.set(0.5);

The button() method's parameters are as follows:

  • The button's x and y coordinates
  • The name of the graphic asset to be displayed for the button
  • A callback function that will be executed when the button is pressed
  • A reference to this to specify the execution context
  • The frames that will be used for the over, out and down events.

Note: The over event is the same as hover, out is when the pointer moves out of the button and down is when the button is pressed.

Now we need to define the startGame() function referenced in the code above:

function startGame() {
    startButton.destroy();
    ball.body.velocity.set(150, -150);
    playing = true;
}

When the button is pressed, we remove the button, sets the ball's initial velocity and set the playing variable to true.

Finally for this section, go back into your create() function, find the ball.body.velocity.set(150, -150); line, and remove it. You only want the ball to move when the button is pressed, not before!

Keeping the paddle still before the game starts

It works as expected, but we can still move the paddle when the game hasn't started yet, which looks a bit silly. To stop this, we can take advantage of the playing variable and make the paddle movable only when the game has started. To do that, adjust the update() function like so:

function update() {
    game.physics.arcade.collide(ball, paddle, ballHitPaddle);
    game.physics.arcade.collide(ball, bricks, ballHitBrick);
    if(playing) {
        paddle.x = game.input.x || game.world.width*0.5;
    }
}

That way the paddle is immovable after everything is loaded and prepared, but before the start of the actual game.

Compare your code

You can check the finished code for this lesson in the live demo below, and play with it to understand better how it works:

Next steps

The last thing we will do in this article series is make the gameplay even more interesting by adding some randomization to the way the ball bounces off the paddle.

« PreviousNext »

Document Tags and Contributors

Tags: 
  • 2D
  • Beginner
  • Buttons
  • Canvas
  • Games
  • JavaScript
  • Phaser
  • Tutorial
 Contributors to this page: chrisdavidmills, end3r
 Last updated by: chrisdavidmills, Mar 14, 2016, 3:43:27 AM
  1. Introduction
    1. Introduction to game development for the Web
    2. Anatomy of a video game
  2. APIs for game development
    1. Canvas
    2. CSS
    3. Full Screen
    4. Gamepad
    5. IndexedDB
    6. JavaScript
    7. Pointer Lock
    8. SVG
    9. Typed Arrays
    10. Web Audio
    11. WebGL
    12. WebRTC
    13. Web Sockets
    14. Web Workers
    15. XmlHttpRequest
  3. Tools
    1. asm.js
    2. Emscripten
    3. WebVR
    4. Gecko profiler
    5. Game engines and tools
  4. Techniques
    1. Using async scripts for asm.js
    2. Optimizing startup performance
    3. Using WebRTC peer-to-peer data channels
    4. Efficient animation for web games
    5. 3D games on the Web
      1. 3D games on the Web overview
      2. Explaining basic 3D theory
      3. Building up a basic demo with Three.js
      4. Building up a basic demo with PlayCanvas
      5. WebVR
    6. Audio for Web Games
    7. 2D collision detection
    8. 3D collision detection
      1. 3D collision detection overview
      2. Bounding volume collision detection with THREE.js
    9. Tiles and tilemaps
      1. Tiles and tilemaps overview
      2. Static maps
      3. Scrolling maps
  5. Workflows
    1. 2D breakout game using pure JavaScript
    2. 2D breakout game using Phaser
    3. 2D maze game with device orientation
  6. Publishing games
    1. Publishing games overview
    2. Game distribution
    3. Game promotion
    4. Game monetization
  7. Examples