コンテンツにスキップ

スタート画面を作る

スタート画面を作る

前の 07 箱を積んで崩す まで、ゲームは1画面だけでした。この節では 「スタート画面」を作り、ボタンを押すとゲームが始まるようにします。そのために、画面ごとに シーン(Scene) という単位でコードを分けます。

今回さわる game/: main.js を書き換え(シーンを2つに分ける)

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

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

Phaser では、タイトル画面・ゲーム本編・結果画面などを シーン という単位で分けて作れます。 この節では2つのシーンを用意します。

  • StartScene … タイトルとスタートボタンを出す画面。
  • GameScene … これまで作ってきたゲーム本編(パチンコ・箱)。

タイトルとボタンを出す StartScene を書きます。ボタンを押すと GameScene へ切り替えます。 続けて、ゲーム本編を GameScene に移し、new Phaser.Game の scene に2つのシーンを登録します(下の見出しで説明します)。 差分の最後で、前の節の scene: { create: ... } が消え、そこにあったコードは GameScene の create に入っています。

main.js
1
new Phaser.Game({
2
type: Phaser.AUTO,
3
width: 720,
4
height: 480,
5
backgroundColor: '#fdf6e3',
6
physics: {
7
default: 'matter',
8
matter: { gravity: { y: 1 } },
9
},
10
scene: {
11
create: function () {
12
const groundX = 360;
13
const groundY = 440;
14
const groundWidth = 720;
15
const groundHeight = 80;
16
const groundTop = groundY - groundHeight / 2; // 地面の上面。物はこの高さに乗る
1
class StartScene extends Phaser.Scene {
2
constructor() {
3
super('Start');
4
}
17
5
18
const ground = this.add.rectangle(
19
groundX,
20
groundY,
21
groundWidth,
22
groundHeight,
23
0x888888,
24
);
6
create() {
7
this.add
8
.text(360, 180, 'パチンコ物理ゲーム', {
9
fontSize: '36px',
10
color: '#5d4037',
11
fontStyle: 'bold',
12
})
13
.setOrigin(0.5);
25
14
26
this.matter.add.gameObject(ground, { isStatic: true });
15
const startText = this.add
16
.text(360, 300, '▶ スタート', {
17
fontSize: '28px',
18
color: '#ffffff',
19
backgroundColor: '#e08e3c',
20
padding: { x: 24, y: 12 },
21
})
22
.setOrigin(0.5)
23
.setInteractive({ useHandCursor: true });
27
24
28
const boxSize = 40;
29
const towerX = 560;
30
for (let i = 0; i < 3; i++) {
31
const boxY = groundTop - boxSize / 2 - i * boxSize;
32
const box = this.add.rectangle(
33
towerX,
34
boxY,
35
boxSize,
36
boxSize,
37
0xdddddd,
38
);
39
box.setStrokeStyle(3, 0x333333);
40
this.matter.add.gameObject(box, { restitution: 0.1 });
41
}
25
startText.on('pointerdown', () => this.scene.start('Game'));
26
}
27
}
42
28
43
const anchor = { x: 140, y: 300 };
44
const power = 0.22; // 引っ張った長さを速さに変える倍率
29
class GameScene extends Phaser.Scene {
30
constructor() {
31
super('Game');
32
}
45
33
46
this.add.circle(anchor.x, anchor.y, 6, 0xbbbbbb);
34
create() {
35
const groundX = 360;
36
const groundY = 440;
37
const groundWidth = 720;
38
const groundHeight = 80;
39
const groundTop = groundY - groundHeight / 2; // 地面の上面。物はこの高さに乗る
47
40
48
const aim = this.add.graphics();
41
const ground = this.add.rectangle(
42
groundX,
43
groundY,
44
groundWidth,
45
groundHeight,
46
0x888888,
47
);
49
48
50
const birdRadius = 18;
51
const bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);
52
bird.setStrokeStyle(3, 0x333333);
49
this.matter.add.gameObject(ground, { isStatic: true });
53
50
54
this.matter.add.gameObject(bird, {
55
shape: {
56
type: 'circle',
57
radius: birdRadius,
58
},
59
restitution: 0.2,
60
});
51
const boxSize = 40;
52
const towerX = 560;
53
for (let i = 0; i < 3; i++) {
54
const boxY = groundTop - boxSize / 2 - i * boxSize;
55
const box = this.add.rectangle(towerX, boxY, boxSize, boxSize, 0xdddddd);
56
box.setStrokeStyle(3, 0x333333);
57
this.matter.add.gameObject(box, { restitution: 0.1 });
58
}
61
59
62
// 待機中は動かないように静的にしておく。
63
bird.setStatic(true);
60
const anchor = { x: 140, y: 300 };
61
const power = 0.22; // 引っ張った長さを速さに変える倍率
64
62
65
let dragging = false;
63
this.add.circle(anchor.x, anchor.y, 6, 0xbbbbbb);
66
64
67
this.input.on('pointerdown', function () {
68
bird.setStatic(true);
69
bird.setPosition(anchor.x, anchor.y);
70
bird.setVelocity(0, 0);
71
dragging = true;
72
});
65
const aim = this.add.graphics();
73
66
74
this.input.on('pointermove', function (pointer) {
75
if (!dragging) return;
67
const birdRadius = 18;
68
const bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);
69
bird.setStrokeStyle(3, 0x333333);
76
70
77
bird.setPosition(pointer.x, pointer.y);
71
this.matter.add.gameObject(bird, {
72
shape: {
73
type: 'circle',
74
radius: birdRadius,
75
},
76
restitution: 0.2,
77
});
78
78
79
const forwardX = anchor.x + (anchor.x - bird.x) * 1.5;
80
const forwardY = anchor.y + (anchor.y - bird.y) * 1.5;
81
aim.clear();
82
aim.lineStyle(2, 0x333333, 0.5);
83
aim.lineBetween(bird.x, bird.y, forwardX, forwardY);
84
});
79
// 待機中は動かないように静的にしておく。
80
bird.setStatic(true);
85
81
86
this.input.on('pointerup', function () {
87
if (!dragging) return;
88
dragging = false;
82
let dragging = false;
89
83
90
aim.clear();
84
this.input.on('pointerdown', () => {
85
bird.setStatic(true);
86
bird.setPosition(anchor.x, anchor.y);
87
bird.setVelocity(0, 0);
88
dragging = true;
89
});
91
90
92
const vx = (anchor.x - bird.x) * power;
93
const vy = (anchor.y - bird.y) * power;
91
this.input.on('pointermove', (pointer) => {
92
if (!dragging) return;
94
93
95
bird.setStatic(false);
96
bird.setVelocity(vx, vy);
97
});
98
},
94
bird.setPosition(pointer.x, pointer.y);
95
96
const forwardX = anchor.x + (anchor.x - bird.x) * 1.5;
97
const forwardY = anchor.y + (anchor.y - bird.y) * 1.5;
98
aim.clear();
99
aim.lineStyle(2, 0x333333, 0.5);
100
aim.lineBetween(bird.x, bird.y, forwardX, forwardY);
101
});
102
103
this.input.on('pointerup', () => {
104
if (!dragging) return;
105
dragging = false;
106
107
aim.clear();
108
109
const vx = (anchor.x - bird.x) * power;
110
const vy = (anchor.y - bird.y) * power;
111
112
bird.setStatic(false);
113
bird.setVelocity(vx, vy);
114
});
115
}
116
}
117
118
new Phaser.Game({
119
type: Phaser.AUTO,
120
width: 720,
121
height: 480,
122
backgroundColor: '#fdf6e3',
123
physics: {
124
default: 'matter',
125
matter: { gravity: { y: 1 } },
99
126
},
127
scene: [StartScene, GameScene],
100
128
});
  • class StartScene extends Phaser.Scene … 1つのシーンを「クラス」として定義します。
  • super('Start') … このシーンに 'Start' という名前をつけます。切り替えるときにこの名前を使います。
  • this.add.text(x, y, '文字', { ... }) … 文字を置きます。setOrigin(0.5) で文字の中心を指定位置に合わせます。
  • setInteractive({ useHandCursor: true }) … 押せるようにし、カーソルを指の形にします。
  • startText.on('pointerdown', () => this.scene.start('Game')) … ボタンが押されたら 'Game' シーンへ切り替えます。

これまで create に書いていたゲーム本編を、GameScene クラスの create の中へそのまま移します。

  • super('Game') … このシーンの名前は 'Game'。StartScene から this.scene.start('Game') で呼ばれます。
  • 入力処理の書き方が少し変わります。function () { ... } の代わりに () => { ... }(アロー関数)を使うと、 中の this がシーンを指したままになるので扱いやすくなります。

最後に、new Phaser.Game の scene に2つのシーンを順番に渡します。最初のものが最初に表示されます。

最初にタイトルとスタートボタンが出ます。ボタンを押すとゲームが始まります。 次の節で、鳥を撃つたびに次の鳥がセットされる「リロード」を作ります。

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

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

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