スタート画面を用意する
いきなりゲームが始まるのではなく、まず「スタート画面」を出して、押したら遊び始める形にします。 そのために 画面(シーン)を 2 つに分ける という Phaser の考え方を導入します。
これまで scene: { create: ... } と 1 つのシーンで書いてきました。この節からは、それぞれの画面を
クラスとして定義し、Phaser.Game に一覧で渡します。今後の節はこの形を土台にします。
今回さわる
game/:main.jsを書き換え
前の節を終えていない人は、ここから始められます。
シーンをクラスにする
Section titled “シーンをクラスにする”Phaser のシーンは Phaser.Scene を継承したクラスで書けます。super('名前') でそのシーンに
名前を付け、create() に画面の中身を書きます。名前は this.scene.start('名前') で他の画面へ
移るときに使います。
スタート画面(StartScene)
Section titled “スタート画面(StartScene)”タイトルと「スタート」ボタンを置き、ボタンが押されたら Game シーンへ移ります。
main.js を変更する
Section titled “main.js を変更する”1new Phaser.Game({2 type: Phaser.AUTO,3 width: 400,4 height: 600,5 backgroundColor: '#fdf6e3',6 physics: {7 default: 'matter',8 matter: { gravity: { y: 1 } },9 },10 scene: {11 create: function () {12 const box = {13 left: 60,14 right: 340,15 top: 150,16 bottom: 540,17 wall: 1218 };1class StartScene extends Phaser.Scene {2 constructor() {3 super('Start');4 }195
20 const g = this.add.graphics();21 g.fillStyle(0xe74c3c, 1);22 g.fillRect(box.left - box.wall, box.top, box.wall, box.bottom - box.top);6 create() {7 this.add8 .text(200, 220, 'スイカゲーム風', {9 fontSize: '32px',10 color: '#8b5a2b',11 fontStyle: 'bold',12 })13 .setOrigin(0.5);2314
24 g.fillStyle(0x2ecc71, 1);25 g.fillRect(box.right, box.top, box.wall, box.bottom - box.top);15 const startText = this.add16 .text(200, 340, '▶ スタート', {17 fontSize: '28px',18 color: '#ffffff',19 backgroundColor: '#e08e3c',20 padding: { x: 24, y: 12 },21 })22 .setOrigin(0.5)23 .setInteractive({ useHandCursor: true });2624
27 g.fillStyle(0x3498db, 1);28 g.fillRect(box.left - box.wall, box.bottom, box.right - box.left + box.wall * 2, box.wall);25 startText.on('pointerdown', () => this.scene.start('Game'));26 }27}2928
30 const wallH = box.bottom - box.top;31 const wallCY = (box.top + box.bottom) / 2;32 this.matter.add.rectangle(box.left - box.wall / 2, wallCY, box.wall, wallH, { isStatic: true });33 this.matter.add.rectangle(box.right + box.wall / 2, wallCY, box.wall, wallH, { isStatic: true });34 const floorW = box.right - box.left + box.wall * 2;35 this.matter.add.rectangle((box.left + box.right) / 2, box.bottom + box.wall / 2, floorW, box.wall, { isStatic: true });29class GameScene extends Phaser.Scene {30 constructor() {31 super('Game');32 }3633
37 this.input.on('pointerdown', (pointer) => {38 const radius = 25;39 const dropY = 100;34 create() {35 const box = {36 left: 60,37 right: 340,38 top: 150,39 bottom: 540,40 wall: 1241 };4042
41 // 箱の内側からはみ出さないよう X を制限する42 const x = Phaser.Math.Clamp(pointer.x, box.left + radius, box.right - radius);43 const g = this.add.graphics();44 g.fillStyle(0xe74c3c, 1);45 g.fillRect(box.left - box.wall, box.top, box.wall, box.bottom - box.top);4346
44 const ball = this.add.circle(x, dropY, radius, 0x333333);47 g.fillStyle(0x2ecc71, 1);48 g.fillRect(box.right, box.top, box.wall, box.bottom - box.top);4549
46 this.matter.add.gameObject(ball, {47 shape: {48 type: 'circle',49 radius: radius50 },51 restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)52 });50 g.fillStyle(0x3498db, 1);51 g.fillRect(box.left - box.wall, box.bottom, box.right - box.left + box.wall * 2, box.wall);52
53 const wallH = box.bottom - box.top;54 const wallCY = (box.top + box.bottom) / 2;55 this.matter.add.rectangle(box.left - box.wall / 2, wallCY, box.wall, wallH, { isStatic: true });56 this.matter.add.rectangle(box.right + box.wall / 2, wallCY, box.wall, wallH, { isStatic: true });57 const floorW = box.right - box.left + box.wall * 2;58 this.matter.add.rectangle((box.left + box.right) / 2, box.bottom + box.wall / 2, floorW, box.wall, { isStatic: true });59
60 this.input.on('pointerdown', (pointer) => {61 const radius = 25;62 const dropY = 100;63
64 // 箱の内側からはみ出さないよう X を制限する65 const x = Phaser.Math.Clamp(pointer.x, box.left + radius, box.right - radius);66
67 const ball = this.add.circle(x, dropY, radius, 0x333333);68
69 this.matter.add.gameObject(ball, {70 shape: {71 type: 'circle',72 radius: radius73 },74 restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)5375 });54 }76 });77 }78}79
80new Phaser.Game({81 type: Phaser.AUTO,82 width: 400,83 height: 600,84 backgroundColor: '#fdf6e3',85 physics: {86 default: 'matter',87 matter: { gravity: { y: 1 } },5588 },89 scene: [StartScene, GameScene],5690});this.add.text(x, y, 文字, スタイル)… 文字を置きます。.setOrigin(0.5)… 文字の中心を (x, y) に合わせます(既定は左上起点)。.setInteractive({ useHandCursor: true })… その文字をクリック可能にし、カーソルを指の形にします。startText.on('pointerdown', ...)… ボタンが押されたらthis.scene.start('Game')で Game 画面へ。
ゲーム画面(GameScene)
Section titled “ゲーム画面(GameScene)”前の節の create の中身を、そのまま GameScene の create() に移します。中身(箱・落とす処理)は
変わりません。
ゲームにシーンを登録する
Section titled “ゲームにシーンを登録する”最後に Phaser.Game の scene に、配列でシーンを渡します。配列の先頭が最初に表示される画面です。
最初にタイトルと「▶ スタート」が出て、押すとゲーム画面に切り替わり、これまで通りボールを落とせます。 画面を分けられたので、次の節で「ゲームオーバー画面」も足せます。