コンテンツにスキップ

スタート画面を用意する

スタート画面を用意する

いきなりゲームが始まるのではなく、まず「スタート画面」を出して、押したら遊び始める形にします。 そのために 画面(シーン)を 2 つに分ける という Phaser の考え方を導入します。

これまで scene: { create: ... } と 1 つのシーンで書いてきました。この節からは、それぞれの画面を クラスとして定義し、Phaser.Game に一覧で渡します。今後の節はこの形を土台にします。

今回さわる game/: main.js を書き換え

前の節を終えていない人は、ここから始められます。

この節を始めるときのファイル

Phaser のシーンは Phaser.Scene を継承したクラスで書けます。super('名前') でそのシーンに 名前を付け、create() に画面の中身を書きます。名前は this.scene.start('名前') で他の画面へ 移るときに使います。

タイトルと「スタート」ボタンを置き、ボタンが押されたら Game シーンへ移ります。

main.js
1
new 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: 12
18
};
1
class StartScene extends Phaser.Scene {
2
constructor() {
3
super('Start');
4
}
19
5
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.add
8
.text(200, 220, 'スイカゲーム風', {
9
fontSize: '32px',
10
color: '#8b5a2b',
11
fontStyle: 'bold',
12
})
13
.setOrigin(0.5);
23
14
24
g.fillStyle(0x2ecc71, 1);
25
g.fillRect(box.right, box.top, box.wall, box.bottom - box.top);
15
const startText = this.add
16
.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 });
26
24
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
}
29
28
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 });
29
class GameScene extends Phaser.Scene {
30
constructor() {
31
super('Game');
32
}
36
33
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: 12
41
};
40
42
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);
43
46
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);
45
49
46
this.matter.add.gameObject(ball, {
47
shape: {
48
type: 'circle',
49
radius: radius
50
},
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: radius
73
},
74
restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)
53
75
});
54
}
76
});
77
}
78
}
79
80
new 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 } },
55
88
},
89
scene: [StartScene, GameScene],
56
90
});
  • this.add.text(x, y, 文字, スタイル) … 文字を置きます。
  • .setOrigin(0.5) … 文字の中心を (x, y) に合わせます(既定は左上起点)。
  • .setInteractive({ useHandCursor: true }) … その文字をクリック可能にし、カーソルを指の形にします。
  • startText.on('pointerdown', ...) … ボタンが押されたら this.scene.start('Game') で Game 画面へ。

前の節の create の中身を、そのまま GameScene の create() に移します。中身(箱・落とす処理)は 変わりません。

最後に Phaser.Game の scene に、配列でシーンを渡します。配列の先頭が最初に表示される画面です。

最初にタイトルと「▶ スタート」が出て、押すとゲーム画面に切り替わり、これまで通りボールを落とせます。 画面を分けられたので、次の節で「ゲームオーバー画面」も足せます。

↗ 新しいタブで開く
このステップの完成イメージ(実際に触って動かせます)

この節を終えたときのディレクトリ

Section titled “この節を終えたときのディレクトリ”
この節を終えたときのファイル