発射したら次の鳥をセットする
前の 08 スタート画面を作る までは、同じ鳥を何度でも引っ張れました。
本物のパチンコゲームは弾(鳥)の数が決まっています。この節では、鳥を撃つと少し待って次の鳥が
セットされる「リロード」を作り、残りの数も表示します。GameScene を書き換えます。
今回さわる
game/:main.jsのGameSceneを書き換え
前の節を終えていない人は、ここから始められます。
残りの鳥を表示する
Section titled “残りの鳥を表示する”残りの鳥の数を覚えておき、左上に小さな丸で並べて見せます。 このあと説明する「鳥を1羽ずつセットする」「引っ張りを、鳥があるときだけにする」「発射したらリロードする」の変更も、 下の差分にまとめて入っています。
main.js を変更する
Section titled “main.js を変更する”5959const anchor = { x: 140, y: 300 };6060const power = 0.22; // 引っ張った長さを速さに変える倍率61const birdRadius = 18;6162
6263this.add.circle(anchor.x, anchor.y, 6, 0xbbbbbb);6364
6465const aim = this.add.graphics();6566
66const birdRadius = 18;67const bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);68bird.setStrokeStyle(3, 0x333333);67let birdsLeft = 5;68const reserve = this.add.graphics();69const drawReserve = () => {70 reserve.clear();71 for (let i = 0; i < birdsLeft; i++) {72 const x = 30 + i * 26;73 reserve.fillStyle(0xffffff, 1);74 reserve.fillCircle(x, 40, 9);75 reserve.lineStyle(2, 0x333333, 1);76 reserve.strokeCircle(x, 40, 9);77 }78};6979
70this.matter.add.gameObject(bird, {71 shape: {72 type: 'circle',73 radius: birdRadius,74 },75 restitution: 0.2,76});80// いま操作できる鳥。発射中やリロード待ちのときは null。81let bird = null;82let dragging = false;7783
78// 待機中は動かないように静的にしておく。79bird.setStatic(true);84const spawnBird = () => {85 if (birdsLeft <= 0) return;86 bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);87 bird.setStrokeStyle(3, 0x333333);88 this.matter.add.gameObject(bird, {89 shape: { type: 'circle', radius: birdRadius },90 restitution: 0.2,91 });92 // 待機中は動かないように静的にしておく。93 bird.setStatic(true);94};8095
81let dragging = false;96drawReserve();97spawnBird();8298
8399this.input.on('pointerdown', () => {84 bird.setStatic(true);85 bird.setPosition(anchor.x, anchor.y);86 bird.setVelocity(0, 0);100 if (!bird || dragging) return;87101 dragging = true;88102});89103
90104this.input.on('pointermove', (pointer) => {91 if (!dragging) return;105 if (!dragging || !bird) return;92106
93107 bird.setPosition(pointer.x, pointer.y);101115 });102116
103117 this.input.on('pointerup', () => {104 if (!dragging) return;118 if (!dragging || !bird) return;105119 dragging = false;106
107120 aim.clear();108121
109122 const vx = (anchor.x - bird.x) * power;110123 const vy = (anchor.y - bird.y) * power;111
112124 bird.setStatic(false);113125 bird.setVelocity(vx, vy);126
127 bird = null;128 birdsLeft -= 1;129 drawReserve();130
131 this.time.delayedCall(1200, () => spawnBird());114132 });115133 }116134}birdsLeft = 5… 残りの鳥の数。撃つたびに減らします。drawReserve… 残りの数だけ、白い小さな丸を横に並べて描く関数です。数が変わるたびに呼び直します。
鳥を1羽ずつセットする
Section titled “鳥を1羽ずつセットする”鳥を毎回作り直す形にします。spawnBird で1羽セットし、発射したら少し待ってまたセットします。
bird = null… いま操作できる鳥。発射した後やリロード待ちのときは「無い(null)」状態にします。spawnBird… パチンコの位置に鳥を1羽用意して静的にします。残りが 0 なら何もしません。- 最初に一度
drawReserve()とspawnBird()を呼んで、残数表示と最初の1羽を出します。
引っ張りを、鳥があるときだけにする
Section titled “引っ張りを、鳥があるときだけにする”鳥は発射してから次がセットされるまでの 1.2 秒、いなくなります。その間にさわられても何も
起きないように、pointerdown と pointermove を直します。
pointerdown は「引っ張り始めの合図」だけにします。鳥は spawnBird がそのつどパチンコの
位置に作るので、押すたびに位置を戻す必要がなくなりました。
if (!bird || dragging) return… 鳥がまだ無いとき、すでに引っ張り中のときは何もしません。- 前の節にあった
bird.setStatic(true)/bird.setPosition(...)/bird.setVelocity(0, 0)の 3行は消します。spawnBirdが静的な鳥をanchorに置いた状態で作るので、押すたびに戻す 仕事がなくなりました。
pointermove は、先頭の1行に !bird を足すだけです。
発射したらリロードする
Section titled “発射したらリロードする”発射したら鳥を手放して残りを1つ減らし、次の1羽をセットするまで待ちます。
if (!dragging || !bird) return… 引っ張り中でないとき、または鳥がまだ無いときは何もしません。- 発射後に
bird = nullとして、飛んでいった鳥をもう操作できないようにします。 birdsLeft -= 1; drawReserve();… 残りを1つ減らして表示を更新します。this.time.delayedCall(1200, () => spawnBird())… 1200ミリ秒(1.2秒)待ってから次の鳥をセットします。
鳥を撃つと少し待って次の鳥がパチンコにセットされ、左上の残り数が1つ減ります。 5羽を撃ち切ると鳥が出なくなります。次の節で、撃ち切ったときにゲームオーバー画面へ進むようにします。