コンテンツにスキップ

パチンコで引っ張って飛ばす

パチンコで引っ張って飛ばす

前の 04 クリックで飛ばす では、クリックすると決まった速さで 鳥が飛びました。でも本物のパチンコは「引っ張った向きと強さ」で飛び方が変わります。 この節では、鳥をドラッグして引っ張り、離すと引いた方向と反対に飛ぶようにします。

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

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

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

まず、鳥が構える「パチンコの位置(基点)」を決めます。離したときは、この位置を基点に 飛んでいきます。目印として薄い丸も置いておきます。 そのあとに鳥を作り直し、「押した」「動かした」「離した」の 3 つの操作を受け取るコードを書きます(下の見出しで順に説明します)。

main.js
25
25
this.matter.add.gameObject(ground, { isStatic: true });
26
26
27
const birdRadius = 18;
27
const anchor = { x: 140, y: 300 };
28
const power = 0.22; // 引っ張った長さを速さに変える倍率
28
29
29
const bird = this.add.circle(
30
140,
31
groundTop - birdRadius,
32
birdRadius,
33
0xffffff,
34
);
30
this.add.circle(anchor.x, anchor.y, 6, 0xbbbbbb);
31
32
const birdRadius = 18;
33
const bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);
35
34
bird.setStrokeStyle(3, 0x333333);
36
35
37
36
this.matter.add.gameObject(bird, {
main.js
43
42
restitution: 0.2,
44
43
});
45
44
45
// 待機中は動かないように静的にしておく。
46
bird.setStatic(true);
47
48
let dragging = false;
49
46
50
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;
48
55
});
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
});
49
73
},
50
74
},
51
75
});
  • anchor … パチンコの位置。鳥はここに戻ってきて、ここを基点に飛びます。
  • power … 引っ張った長さ(ピクセル)を速さに変えるときの倍率。大きいほど強く飛びます。

鳥を anchor の位置に作り、引っ張っている間は落ちてほしくないので、いったん静的 (動かない状態)にしておきます。前の節で鳥を作っていた部分を置き換えています。

  • setStatic(true) … 物理の体を「動かない」状態にします。重力で落ちなくなり、引っ張る間もその場に とどまります。離すときに setStatic(false) へ戻して、また物理で動くようにします。

マウス(指)の動きを3つのタイミングで受け取ります。押した・動かした・離した、の3つです。 まず「押した」と「動かした」です。前の節の pointerdown は置き換えています。

  • dragging … いま引っ張っている最中かどうかを覚えておく印です。
  • pointerdown … 押した瞬間。鳥をパチンコの位置に戻し、速度を 0 にして、引っ張りを開始します。
  • pointermove … 動かしている間だけ(dragging が true のとき)反応します。
  • bird.setPosition(pointer.x, pointer.y) … 指のいる位置へ鳥を移します。pointer.x / pointer.y は ゲーム画面の座標でそのまま届くので、変換せずに渡せます。

離した瞬間に、引っ張った向きと反対へ、引いた長さに応じた速さで飛ばします(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);
});
},
},
});
この節を終えたときのファイル