コンテンツにスキップ

タップした場所にボールを追加する

タップした場所にボールを追加する

ここまではボールが 1 個だけでした。この節では画面をタップ(クリック)するたびに、その場所へ ボールを 1 個追加します。ボール同士もぶつかって積み上がります。

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

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

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

ボール生成を「タップされたとき」に動かす

Section titled “ボール生成を「タップされたとき」に動かす”

前の節では create の中で 1 回だけボールを作っていました。その部分を、入力イベントの中へ移します。

this.input.on('pointerdown', (pointer) => { ... }) は「クリック/タップされたら中の処理を実行する」 という登録です。pointer.x / pointer.y に押された座標が入っています。

main.js
34
34
const floorW = box.right - box.left + box.wall * 2;
35
35
this.matter.add.rectangle((box.left + box.right) / 2, box.bottom + box.wall / 2, floorW, box.wall, { isStatic: true });
36
36
37
const radius = 25;
38
const ball = this.add.circle(200, 80, radius, 0x333333);
37
this.input.on('pointerdown', (pointer) => {
38
const radius = 25;
39
const ball = this.add.circle(pointer.x, pointer.y, radius, 0x333333);
39
40
40
this.matter.add.gameObject(ball, {
41
shape: {
42
type: 'circle',
43
radius: radius
44
},
45
restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)
41
this.matter.add.gameObject(ball, {
42
shape: {
43
type: 'circle',
44
radius: radius
45
},
46
restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)
47
});
46
48
});
47
49
}
48
50
},

やっていることは前の節の「ボールを 1 個作る」処理と同じで、座標だけ pointer.x, pointer.y に 変えて、タップのたびに呼ばれるようにしただけです。

create は最初に 1 回だけ呼ばれます。「タップのたびに何かする」ような繰り返し起きることは、 このようにイベント(pointerdown など)に登録します。

箱の中をクリックすると、その位置にボールが現れて落ちます。何度もクリックすればボールが増え、 互いにぶつかって積み重なります。ただし今は箱の外や壁の上でも出せてしまいます。次の節で 「上から狙って落とす」形に整えます。

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

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

Section titled “この節を終えたときのディレクトリ”
game
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>05 タップした場所にボールを追加</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="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: {
create: function () {
const box = {
left: 60,
right: 340,
top: 150,
bottom: 540,
wall: 12
};
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);
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) => {
const radius = 25;
const ball = this.add.circle(pointer.x, pointer.y, radius, 0x333333);
this.matter.add.gameObject(ball, {
shape: {
type: 'circle',
radius: radius
},
restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)
});
});
}
},
});
この節を終えたときのファイル