パチンコで引っ張って飛ばす
前の 04 クリックで飛ばす では、クリックすると決まった速さで 鳥が飛びました。でも本物のパチンコは「引っ張った向きと強さ」で飛び方が変わります。 この節では、鳥をドラッグして引っ張り、離すと引いた方向と反対に飛ぶようにします。
今回さわる
game/:main.jsを書き換え
前の節を終えていない人は、ここから始められます。
パチンコの位置を決める
Section titled “パチンコの位置を決める”まず、鳥が構える「パチンコの位置(基点)」を決めます。離したときは、この位置を基点に 飛んでいきます。目印として薄い丸も置いておきます。 そのあとに鳥を作り直し、「押した」「動かした」「離した」の 3 つの操作を受け取るコードを書きます(下の見出しで順に説明します)。
main.js を変更する
Section titled “main.js を変更する”2525this.matter.add.gameObject(ground, { isStatic: true });2626
27const birdRadius = 18;27const anchor = { x: 140, y: 300 };28const power = 0.22; // 引っ張った長さを速さに変える倍率2829
29const bird = this.add.circle(30 140,31 groundTop - birdRadius,32 birdRadius,33 0xffffff,34);30this.add.circle(anchor.x, anchor.y, 6, 0xbbbbbb);31
32const birdRadius = 18;33const bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);3534bird.setStrokeStyle(3, 0x333333);3635
3736this.matter.add.gameObject(bird, {4342 restitution: 0.2,4443 });4544
45 // 待機中は動かないように静的にしておく。46 bird.setStatic(true);47
48 let dragging = false;49
4650 this.input.on('pointerdown', function () {47 bird.setVelocity(12, -12);51 bird.setStatic(true);52 bird.setPosition(anchor.x, anchor.y);53 bird.setVelocity(0, 0);54 dragging = true;4855 });56
57 this.input.on('pointermove', function (pointer) {58 if (!dragging) return;59
60 bird.setPosition(pointer.x, pointer.y);61 });62
63 this.input.on('pointerup', function () {64 if (!dragging) return;65 dragging = false;66
67 const vx = (anchor.x - bird.x) * power;68 const vy = (anchor.y - bird.y) * power;69
70 bird.setStatic(false);71 bird.setVelocity(vx, vy);72 });4973 },5074 },5175});anchor… パチンコの位置。鳥はここに戻ってきて、ここを基点に飛びます。power… 引っ張った長さ(ピクセル)を速さに変えるときの倍率。大きいほど強く飛びます。
鳥を「待機」させる
Section titled “鳥を「待機」させる”鳥を anchor の位置に作り、引っ張っている間は落ちてほしくないので、いったん静的
(動かない状態)にしておきます。前の節で鳥を作っていた部分を置き換えています。
setStatic(true)… 物理の体を「動かない」状態にします。重力で落ちなくなり、引っ張る間もその場に とどまります。離すときにsetStatic(false)へ戻して、また物理で動くようにします。
マウス(指)の動きを3つのタイミングで受け取ります。押した・動かした・離した、の3つです。
まず「押した」と「動かした」です。前の節の pointerdown は置き換えています。
dragging… いま引っ張っている最中かどうかを覚えておく印です。pointerdown… 押した瞬間。鳥をパチンコの位置に戻し、速度を 0 にして、引っ張りを開始します。pointermove… 動かしている間だけ(draggingがtrueのとき)反応します。bird.setPosition(pointer.x, pointer.y)… 指のいる位置へ鳥を移します。pointer.x/pointer.yは ゲーム画面の座標でそのまま届くので、変換せずに渡せます。
離して飛ばす
Section titled “離して飛ばす”離した瞬間に、引っ張った向きと反対へ、引いた長さに応じた速さで飛ばします(pointermove の後の pointerup)。
(anchor.x - bird.x)… 「パチンコの位置 − いまの鳥の位置」なので、引っ張った向きと反対の向きになります。 これにpowerを掛けて速さにします。setStatic(false)… 静的をやめて、また物理で動くようにします。setVelocity(vx, vy)… その速さで飛び出します。
図: パチンコの位置から引いたベクトル(bird → anchor)と反対向きに、長さに比例した速さで発射する。
ブラウザで開くと、鳥をドラッグして引っ張れます。離すと、引いた向きと反対にビュンと飛びます。 強く引くほど速く飛びます。まだ狙いの線がないので、次の節で引っ張り中に飛ぶ方向の線を出します。
この節を終えたときのディレクトリ
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; 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 anchor = { x: 140, y: 300 }; const power = 0.22; // 引っ張った長さを速さに変える倍率
this.add.circle(anchor.x, anchor.y, 6, 0xbbbbbb);
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); });
this.input.on('pointerup', function () { if (!dragging) return; dragging = false;
const vx = (anchor.x - bird.x) * power; const vy = (anchor.y - bird.y) * power;
bird.setStatic(false); bird.setVelocity(vx, vy); }); }, },});