狙いの線を表示する
前の 05 パチンコで引っ張って飛ばす で、引っ張って飛ばせるように なりました。でも、どっちへ飛ぶのか分かりにくいです。この節では、引っ張っている間に「飛ぶ方向の線」を 表示します。
今回さわる
game/:main.jsを書き換え
前の節を終えていない人は、ここから始められます。
引っ張っている間だけ線を引く
Section titled “引っ張っている間だけ線を引く”狙いの線を描くための専用の「お絵かき道具」を1つ用意します。毎フレーム描き直すので、変数に持っておきます。
引っ張っている間(pointermove)にこの道具で線を描き、離したとき(pointerup)に線を消します。
main.js を変更する
Section titled “main.js を変更する”3030this.add.circle(anchor.x, anchor.y, 6, 0xbbbbbb);3131
32const aim = this.add.graphics();33
3234const birdRadius = 18;3335const bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);3436bird.setStrokeStyle(3, 0x333333);5860 if (!dragging) return;5961
6062 bird.setPosition(pointer.x, pointer.y);63
64 const forwardX = anchor.x + (anchor.x - bird.x) * 1.5;65 const forwardY = anchor.y + (anchor.y - bird.y) * 1.5;66 aim.clear();67 aim.lineStyle(2, 0x333333, 0.5);68 aim.lineBetween(bird.x, bird.y, forwardX, forwardY);6169});6270
6371this.input.on('pointerup', function () {6472 if (!dragging) return;6573 dragging = false;6674
75 aim.clear();76
6777 const vx = (anchor.x - bird.x) * power;6878 const vy = (anchor.y - bird.y) * power;forwardX/forwardY… パチンコの位置から「引っ張った向きと反対(=飛ぶ向き)」へ、少し先の点を計算します。aim.clear()… 前のフレームで引いた線を消します。これをしないと線が残り続けてしまいます。aim.lineStyle(2, 0x333333, 0.5)… 線の太さ 2・色(濃いグレー)・濃さ 0.5(半透明)を決めます。aim.lineBetween(...)… 鳥の位置からforwardの点まで、まっすぐな線を引きます。pointerupの中のaim.clear()… 離したときに、狙いの線を消します。
引っ張っている間、鳥から飛ぶ方向へ線が伸びます。離すと線が消えて、その向きへ飛びます。 狙いがつけやすくなりました。次の節で、飛ばす相手(崩せる箱)を用意します。
この節を終えたときのディレクトリ
Section titled “この節を終えたときのディレクトリ”game
<!doctype html><html lang="ja"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>06 狙いの線を表示する</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 aim = this.add.graphics();
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);
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', function () { if (!dragging) 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); }); }, },});