コンテンツにスキップ

箱を積んで崩す

箱を積んで崩す

前の 06 狙いの線を表示する までで、狙って飛ばせるようになりました。 この節では、右側に箱を積みます。箱は物理で動くので、鳥をぶつけると崩れます。

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

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

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

箱を縦に3つ積むコードを足します。

main.js
25
25
this.matter.add.gameObject(ground, { isStatic: true });
26
26
27
const boxSize = 40;
28
const towerX = 560;
29
for (let i = 0; i < 3; i++) {
30
const boxY = groundTop - boxSize / 2 - i * boxSize;
31
const box = this.add.rectangle(
32
towerX,
33
boxY,
34
boxSize,
35
boxSize,
36
0xdddddd,
37
);
38
box.setStrokeStyle(3, 0x333333);
39
this.matter.add.gameObject(box, { restitution: 0.1 });
40
}
41
27
42
const anchor = { x: 140, y: 300 };
28
43
const power = 0.22; // 引っ張った長さを速さに変える倍率
  • boxSize = 40 … 箱1つの大きさ(40×40)です。
  • towerX = 560 … 箱を積む横位置(画面の右寄り)です。
  • for (let i = 0; i < 3; i++) … 3回くり返して、箱を縦に3つ作ります。
  • boxY = groundTop - boxSize / 2 - i * boxSize … i 番目の箱の高さ。地面の上面(groundTop)から 順に積み上がるよう計算しています。
  • this.add.rectangle(...) … 薄いグレーの四角(箱の見た目)を作り、濃い輪郭線をつけます。
  • this.matter.add.gameObject(box, { restitution: 0.1 }) … 箱を物理ボディにします。shape を省くと、 見た目の四角と同じ大きさの当たり判定が自動でつきます。isStatic を付けていないので、この箱は動きます。

3つの箱の boxY は、下から順に 380・340・300 です。this.add.rectangle() は中心の位置を 受け取るので、地面の上にぴったり乗せるには箱の半分(20)だけ上げます。そこから 1 段上がるたびに boxSize の 40 を引いて、y を小さくしていきます。

箱は中心ぞろえ。いちばん下が y=380 で、1 段上がるごとに 40 ずつ小さくなる

図: y は下が大きいので、上に積むほど数字は小さくなる。

右側に箱が3つ積まれます。鳥を引っ張って箱にぶつけると、箱が崩れて散らばります。 狙って崩す手ごたえが出てきました。次は、ゲームらしく「スタート画面」を用意します。

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

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

Section titled “この節を終えたときのディレクトリ”
game
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>07 箱を積んで崩す</title>
<style>
body {
margin: 0;
overflow: hidden;
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="main.js" defer></script>
</head>
<body></body>
</html>
new Phaser.Game({
type: Phaser.AUTO,
width: 720,
height: 480,
backgroundColor: '#fdf6e3',
physics: {
default: 'matter',
matter: { gravity: { y: 1 } },
},
scene: {
create: function () {
const groundX = 360;
const groundY = 440;
const groundWidth = 720;
const groundHeight = 80;
const groundTop = groundY - groundHeight / 2; // 地面の上面。物はこの高さに乗る
const ground = this.add.rectangle(
groundX,
groundY,
groundWidth,
groundHeight,
0x888888,
);
this.matter.add.gameObject(ground, { isStatic: true });
const boxSize = 40;
const towerX = 560;
for (let i = 0; i < 3; i++) {
const boxY = groundTop - boxSize / 2 - i * boxSize;
const box = this.add.rectangle(
towerX,
boxY,
boxSize,
boxSize,
0xdddddd,
);
box.setStrokeStyle(3, 0x333333);
this.matter.add.gameObject(box, { restitution: 0.1 });
}
const anchor = { x: 140, y: 300 };
const power = 0.22; // 引っ張った長さを速さに変える倍率
this.add.circle(anchor.x, anchor.y, 6, 0xbbbbbb);
const aim = this.add.graphics();
const birdRadius = 18;
const bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);
bird.setStrokeStyle(3, 0x333333);
this.matter.add.gameObject(bird, {
shape: {
type: 'circle',
radius: birdRadius,
},
restitution: 0.2,
});
// 待機中は動かないように静的にしておく。
bird.setStatic(true);
let dragging = false;
this.input.on('pointerdown', function () {
bird.setStatic(true);
bird.setPosition(anchor.x, anchor.y);
bird.setVelocity(0, 0);
dragging = true;
});
this.input.on('pointermove', function (pointer) {
if (!dragging) return;
bird.setPosition(pointer.x, pointer.y);
const forwardX = anchor.x + (anchor.x - bird.x) * 1.5;
const forwardY = anchor.y + (anchor.y - bird.y) * 1.5;
aim.clear();
aim.lineStyle(2, 0x333333, 0.5);
aim.lineBetween(bird.x, bird.y, forwardX, forwardY);
});
this.input.on('pointerup', function () {
if (!dragging) return;
dragging = false;
aim.clear();
const vx = (anchor.x - bird.x) * power;
const vy = (anchor.y - bird.y) * power;
bird.setStatic(false);
bird.setVelocity(vx, vy);
});
},
},
});
この節を終えたときのファイル