コンテンツにスキップ

発射したら次の鳥をセットする

発射したら次の鳥をセットする

前の 08 スタート画面を作る までは、同じ鳥を何度でも引っ張れました。 本物のパチンコゲームは弾(鳥)の数が決まっています。この節では、鳥を撃つと少し待って次の鳥が セットされる「リロード」を作り、残りの数も表示します。GameScene を書き換えます。

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

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

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

残りの鳥の数を覚えておき、左上に小さな丸で並べて見せます。 このあと説明する「鳥を1羽ずつセットする」「引っ張りを、鳥があるときだけにする」「発射したらリロードする」の変更も、 下の差分にまとめて入っています。

main.js
59
59
const anchor = { x: 140, y: 300 };
60
60
const power = 0.22; // 引っ張った長さを速さに変える倍率
61
const birdRadius = 18;
61
62
62
63
this.add.circle(anchor.x, anchor.y, 6, 0xbbbbbb);
63
64
64
65
const aim = this.add.graphics();
65
66
66
const birdRadius = 18;
67
const bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);
68
bird.setStrokeStyle(3, 0x333333);
67
let birdsLeft = 5;
68
const reserve = this.add.graphics();
69
const 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
};
69
79
70
this.matter.add.gameObject(bird, {
71
shape: {
72
type: 'circle',
73
radius: birdRadius,
74
},
75
restitution: 0.2,
76
});
80
// いま操作できる鳥。発射中やリロード待ちのときは null。
81
let bird = null;
82
let dragging = false;
77
83
78
// 待機中は動かないように静的にしておく。
79
bird.setStatic(true);
84
const 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
};
80
95
81
let dragging = false;
96
drawReserve();
97
spawnBird();
82
98
83
99
this.input.on('pointerdown', () => {
84
bird.setStatic(true);
85
bird.setPosition(anchor.x, anchor.y);
86
bird.setVelocity(0, 0);
100
if (!bird || dragging) return;
87
101
dragging = true;
88
102
});
89
103
90
104
this.input.on('pointermove', (pointer) => {
91
if (!dragging) return;
105
if (!dragging || !bird) return;
92
106
93
107
bird.setPosition(pointer.x, pointer.y);
main.js
101
115
});
102
116
103
117
this.input.on('pointerup', () => {
104
if (!dragging) return;
118
if (!dragging || !bird) return;
105
119
dragging = false;
106
107
120
aim.clear();
108
121
109
122
const vx = (anchor.x - bird.x) * power;
110
123
const vy = (anchor.y - bird.y) * power;
111
112
124
bird.setStatic(false);
113
125
bird.setVelocity(vx, vy);
126
127
bird = null;
128
birdsLeft -= 1;
129
drawReserve();
130
131
this.time.delayedCall(1200, () => spawnBird());
114
132
});
115
133
}
116
134
}
  • birdsLeft = 5 … 残りの鳥の数。撃つたびに減らします。
  • drawReserve … 残りの数だけ、白い小さな丸を横に並べて描く関数です。数が変わるたびに呼び直します。

鳥を毎回作り直す形にします。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 を足すだけです。

発射したら鳥を手放して残りを1つ減らし、次の1羽をセットするまで待ちます。

  • if (!dragging || !bird) return … 引っ張り中でないとき、または鳥がまだ無いときは何もしません。
  • 発射後に bird = null として、飛んでいった鳥をもう操作できないようにします。
  • birdsLeft -= 1; drawReserve(); … 残りを1つ減らして表示を更新します。
  • this.time.delayedCall(1200, () => spawnBird()) … 1200ミリ秒(1.2秒)待ってから次の鳥をセットします。

鳥を撃つと少し待って次の鳥がパチンコにセットされ、左上の残り数が1つ減ります。 5羽を撃ち切ると鳥が出なくなります。次の節で、撃ち切ったときにゲームオーバー画面へ進むようにします。

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

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

Section titled “この節を終えたときのディレクトリ”
この節を終えたときのファイル