コンテンツにスキップ

合体すると大きくなる

合体すると大きくなる

前の節では合体しても大きさは同じでした。この節では、合体したらひと回り大きいボールを 新しく作り直すようにします。スイカゲームの「合体で育つ」核です。

合体では「両方消して、少し大きい 1 個を中間の位置に作る」という流れにします。ボールを作る処理は これから何度も使うので、まずメソッドに切り出します。

今回さわる game/: scenes/game-scene.js を書き換え

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

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

ボール生成をメソッドにまとめる

Section titled “ボール生成をメソッドにまとめる”

create() の中に直接書いていた「円を作って物理ボディにする」を、spawnBall(x, y, radius) に まとめます。半径は判定でも使うので、ボール自身に radius を持たせておきます。

scenes/game-scene.js
13
13
bottom: 540,
14
14
wall: 12
15
15
};
16
const ballRadius = 20;
17
const grow = 10;
16
18
17
this.radius = 25;
18
19
this.lineY = 190;
19
20
this.balls = [];
20
21
this.pendingRemoval = new Set();
22
this.pendingMerge = [];
21
23
this.isOver = false;
22
24
23
25
const g = this.add.graphics();
scenes/game-scene.js
49
51
const dropY = 100;
50
52
51
53
// 箱の内側からはみ出さないよう X を制限する
52
const x = Phaser.Math.Clamp(pointer.x, box.left + this.radius, box.right - this.radius);
54
const x = Phaser.Math.Clamp(pointer.x, box.left + ballRadius, box.right - ballRadius);
53
55
54
const ball = this.add.circle(x, dropY, this.radius, 0x333333);
55
this.matter.add.gameObject(ball, {
56
shape: {
57
type: 'circle',
58
radius: this.radius
59
},
60
restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)
61
});
62
56
const ball = this.spawnBall(x, dropY, ballRadius);
63
57
this.balls.push(ball);
64
58
});
65
59
66
60
this.matter.world.on('collisionstart', (event) => {
67
61
for (const pair of event.pairs) {
68
// gameObject を持つのがボール(壁は持たない)
69
62
const ballA = pair.bodyA.gameObject;
70
63
const ballB = pair.bodyB.gameObject;
71
64
if (!ballA || !ballB) continue;
72
65
if (this.pendingRemoval.has(ballA) || this.pendingRemoval.has(ballB)) continue;
66
67
this.pendingRemoval.add(ballA);
73
68
this.pendingRemoval.add(ballB);
69
this.pendingMerge.push({
70
x: (ballA.x + ballB.x) / 2,
71
y: (ballA.y + ballB.y) / 2,
72
radius: ballA.radius + grow,
73
});
74
74
}
75
75
});
76
76
}
77
77
78
spawnBall(x, y, radius) {
79
const ball = this.add.circle(x, y, radius, 0x333333);
80
ball.radius = radius; // 線の判定などで使うので半径を持たせておく
81
this.matter.add.gameObject(ball, {
82
shape: {
83
type: 'circle',
84
radius: radius
85
},
86
restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)
87
});
88
return ball;
89
}
90
78
91
update(time, delta) {
79
92
if (this.pendingRemoval.size > 0) {
80
93
for (const ball of this.pendingRemoval) {
scenes/game-scene.js
84
97
this.pendingRemoval.clear();
85
98
}
86
99
100
if (this.pendingMerge.length > 0) {
101
for (const m of this.pendingMerge) {
102
const ball = this.spawnBall(m.x, m.y, m.radius);
103
this.balls.push(ball);
104
}
105
this.pendingMerge.length = 0;
106
}
107
87
108
if (this.isOver) return;
88
109
89
110
// 落下中は速いので、止まって線を超え続けたときだけゲームオーバー(誤検出を防ぐ)
90
111
for (const ball of this.balls) {
91
112
if (!ball.body) continue;
92
113
const isSlow = ball.body.speed < 1;
93
const top = ball.y - this.radius;
114
const top = ball.y - ball.radius;
94
115
if (!isSlow || top >= this.lineY) {
95
116
ball.overLineTime = 0;
96
117
continue;

create() の落とす処理は、このメソッドを呼ぶだけになります。半径の基準値 ballRadius(20)と 合体で足す量 grow(10)も用意します。

X を箱に収める Clamp も、this.radius ではなく ballRadius を使うように直します。

消すのと同じように、「新しく作る大きいボール」もその場では作らず予定に貯めます。 create() の状態に this.pendingMerge = [] を足し、衝突処理で 両方を消す予定にしつつ、 合体後のボールの情報を積みます。

中間の位置に、radius + grow の大きさで作る、という予定です。

予定どおり新しいボールを作る

Section titled “予定どおり新しいボールを作る”

update() で、消す処理のあとに合体予定を処理します。

ゲームオーバー判定の top も、固定の this.radius ではなく、ボールごとの ball.radius を 使うように直します(const top = ball.y - ball.radius;)。

同じボールをぶつけると、消えてひと回り大きいボールが生まれます。何度か重ねて、だんだん 大きく育つのを確かめてください。次の節で、大きさに応じて色を変えます。

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

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

Section titled “この節を終えたときのディレクトリ”
game
// ゲーム本編
class GameScene extends Phaser.Scene {
constructor() {
super('Game');
}
create() {
const box = {
left: 60,
right: 340,
top: 150,
bottom: 540,
wall: 12
};
const ballRadius = 20;
const grow = 10;
this.lineY = 190;
this.balls = [];
this.pendingRemoval = new Set();
this.pendingMerge = [];
this.isOver = false;
const g = this.add.graphics();
g.fillStyle(0xe74c3c, 1);
g.fillRect(box.left - box.wall, box.top, box.wall, box.bottom - box.top);
g.fillStyle(0x2ecc71, 1);
g.fillRect(box.right, box.top, box.wall, box.bottom - box.top);
g.fillStyle(0x3498db, 1);
g.fillRect(box.left - box.wall, box.bottom, box.right - box.left + box.wall * 2, box.wall);
g.lineStyle(2, 0xc0392b, 0.8);
g.beginPath();
g.moveTo(box.left, this.lineY);
g.lineTo(box.right, this.lineY);
g.strokePath();
const wallH = box.bottom - box.top;
const wallCY = (box.top + box.bottom) / 2;
this.matter.add.rectangle(box.left - box.wall / 2, wallCY, box.wall, wallH, { isStatic: true });
this.matter.add.rectangle(box.right + box.wall / 2, wallCY, box.wall, wallH, { isStatic: true });
const floorW = box.right - box.left + box.wall * 2;
this.matter.add.rectangle((box.left + box.right) / 2, box.bottom + box.wall / 2, floorW, box.wall, { isStatic: true });
this.input.on('pointerdown', (pointer) => {
if (this.isOver) return;
const dropY = 100;
// 箱の内側からはみ出さないよう X を制限する
const x = Phaser.Math.Clamp(pointer.x, box.left + ballRadius, box.right - ballRadius);
const ball = this.spawnBall(x, dropY, ballRadius);
this.balls.push(ball);
});
this.matter.world.on('collisionstart', (event) => {
for (const pair of event.pairs) {
const ballA = pair.bodyA.gameObject;
const ballB = pair.bodyB.gameObject;
if (!ballA || !ballB) continue;
if (this.pendingRemoval.has(ballA) || this.pendingRemoval.has(ballB)) continue;
this.pendingRemoval.add(ballA);
this.pendingRemoval.add(ballB);
this.pendingMerge.push({
x: (ballA.x + ballB.x) / 2,
y: (ballA.y + ballB.y) / 2,
radius: ballA.radius + grow,
});
}
});
}
spawnBall(x, y, radius) {
const ball = this.add.circle(x, y, radius, 0x333333);
ball.radius = radius; // 線の判定などで使うので半径を持たせておく
this.matter.add.gameObject(ball, {
shape: {
type: 'circle',
radius: radius
},
restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)
});
return ball;
}
update(time, delta) {
if (this.pendingRemoval.size > 0) {
for (const ball of this.pendingRemoval) {
Phaser.Utils.Array.Remove(this.balls, ball);
ball.destroy();
}
this.pendingRemoval.clear();
}
if (this.pendingMerge.length > 0) {
for (const m of this.pendingMerge) {
const ball = this.spawnBall(m.x, m.y, m.radius);
this.balls.push(ball);
}
this.pendingMerge.length = 0;
}
if (this.isOver) return;
// 落下中は速いので、止まって線を超え続けたときだけゲームオーバー(誤検出を防ぐ)
for (const ball of this.balls) {
if (!ball.body) continue;
const isSlow = ball.body.speed < 1;
const top = ball.y - ball.radius;
if (!isSlow || top >= this.lineY) {
ball.overLineTime = 0;
continue;
}
ball.overLineTime = (ball.overLineTime ?? 0) + delta;
if (ball.overLineTime > 1000) {
this.isOver = true;
this.scene.start('GameOver');
return;
}
}
}
}
// ゲームオーバー画面
class GameOverScene extends Phaser.Scene {
constructor() {
super('GameOver');
}
create() {
this.add
.text(200, 240, 'ゲームオーバー', {
fontSize: '32px',
color: '#c0392b',
fontStyle: 'bold',
})
.setOrigin(0.5);
this.add
.text(200, 320, 'クリックでスタートに戻る', {
fontSize: '18px',
color: '#555555',
})
.setOrigin(0.5);
this.input.once('pointerdown', () => this.scene.start('Start'));
}
}
// スタート画面
class StartScene extends Phaser.Scene {
constructor() {
super('Start');
}
create() {
this.add
.text(200, 220, 'スイカゲーム風', {
fontSize: '32px',
color: '#8b5a2b',
fontStyle: 'bold',
})
.setOrigin(0.5);
const startText = this.add
.text(200, 340, '▶ スタート', {
fontSize: '28px',
color: '#ffffff',
backgroundColor: '#e08e3c',
padding: { x: 24, y: 12 },
})
.setOrigin(0.5)
.setInteractive({ useHandCursor: true });
startText.on('pointerdown', () => this.scene.start('Game'));
}
}
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>12 合体すると大きいボールになる(一定値を足す)</title>
<style>
body {
margin: 0;
display: grid;
place-items: center;
min-height: 100vh;
background: #222;
}
canvas {
display: block;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.90.0/phaser.min.js"></script>
<script src="scenes/start-scene.js" defer></script>
<script src="scenes/game-scene.js" defer></script>
<script src="scenes/gameover-scene.js" defer></script>
<script src="main.js" defer></script>
</head>
<body></body>
</html>
// 合体すると半径に一定値を足した大きいボールになる。
new Phaser.Game({
type: Phaser.AUTO,
width: 400,
height: 600,
backgroundColor: '#fdf6e3',
physics: {
default: 'matter',
matter: { gravity: { y: 1 } },
},
scene: [StartScene, GameScene, GameOverScene],
});
この節を終えたときのファイル