Buttons - Game development 编辑

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:112 次

字数:5900

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文