発射したら次の鳥をセットする
前の 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羽を撃ち切ると鳥が出なくなります。次の節で、撃ち切ったときにゲームオーバー画面へ進むようにします。
この節を終えたときのディレクトリ
Section titled “この節を終えたときのディレクトリ”game
<!doctype html><html lang="ja"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>09 発射したら次の鳥をセットする</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>class StartScene extends Phaser.Scene { constructor() { super('Start'); }
create() { this.add .text(360, 180, 'パチンコ物理ゲーム', { fontSize: '36px', color: '#5d4037', fontStyle: 'bold', }) .setOrigin(0.5);
const startText = this.add .text(360, 300, '▶ スタート', { fontSize: '28px', color: '#ffffff', backgroundColor: '#e08e3c', padding: { x: 24, y: 12 }, }) .setOrigin(0.5) .setInteractive({ useHandCursor: true });
startText.on('pointerdown', () => this.scene.start('Game')); }}
class GameScene extends Phaser.Scene { constructor() { super('Game'); }
create() { 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; // 引っ張った長さを速さに変える倍率 const birdRadius = 18;
this.add.circle(anchor.x, anchor.y, 6, 0xbbbbbb);
const aim = this.add.graphics();
let birdsLeft = 5; const reserve = this.add.graphics(); const drawReserve = () => { reserve.clear(); for (let i = 0; i < birdsLeft; i++) { const x = 30 + i * 26; reserve.fillStyle(0xffffff, 1); reserve.fillCircle(x, 40, 9); reserve.lineStyle(2, 0x333333, 1); reserve.strokeCircle(x, 40, 9); } };
// いま操作できる鳥。発射中やリロード待ちのときは null。 let bird = null; let dragging = false;
const spawnBird = () => { if (birdsLeft <= 0) return; 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); };
drawReserve(); spawnBird();
this.input.on('pointerdown', () => { if (!bird || dragging) return; dragging = true; });
this.input.on('pointermove', (pointer) => { if (!dragging || !bird) 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', () => { if (!dragging || !bird) 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);
bird = null; birdsLeft -= 1; drawReserve();
this.time.delayedCall(1200, () => spawnBird()); }); }}
new Phaser.Game({ type: Phaser.AUTO, width: 720, height: 480, backgroundColor: '#fdf6e3', physics: { default: 'matter', matter: { gravity: { y: 1 } }, }, scene: [StartScene, GameScene],});