X はタップ位置・Y は固定で落とす
本家スイカゲームは「上から横方向を狙って落とす」操作です。前の節では押した場所にそのまま ボールが出ていましたが、この節では 横(X)はタップ位置・縦(Y)は画面上部の固定値にして、 必ず上から落ちるようにします。
今回さわる
game/:main.jsを書き換え
前の節を終えていない人は、ここから始められます。
落とす高さを固定し、X を箱の内側に収める
Section titled “落とす高さを固定し、X を箱の内側に収める”pointerdown の中を書き換えます。変更点は 2 つです。
- 落とす高さ
dropYを固定値(100)にする - タップの X が箱からはみ出さないよう、左右の内側に収める
main.js を変更する
Section titled “main.js を変更する”3636this.input.on('pointerdown', (pointer) => {3737 const radius = 25;38 const ball = this.add.circle(pointer.x, pointer.y, radius, 0x333333);38 const dropY = 100;3939
40 // 箱の内側からはみ出さないよう X を制限する41 const x = Phaser.Math.Clamp(pointer.x, box.left + radius, box.right - radius);42
43 const ball = this.add.circle(x, dropY, radius, 0x333333);44
4045 this.matter.add.gameObject(ball, {4146 shape: {4247 type: 'circle',Phaser.Math.Clamp(値, 最小, 最大) は、値が範囲からはみ出したら端に丸めてくれる関数です。
box.left + radius 〜 box.right - radius に収めることで、ボールの端が壁にめり込まない位置から
落とせます。Y は dropY(=100)に固定なので、どこをクリックしても同じ高さから落ちます。
箱の上のあたりをクリックすると、その横位置から上部の一定の高さでボールが現れて落ちます。 左右にずらしながらクリックして、狙った場所に積む感覚を確かめてください。
これで「落とす」操作の土台ができました。次の節からは画面(シーン)を分けて、スタート画面を 用意していきます。
この節を終えたときのディレクトリ
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 X はタップ位置・Y は固定で落とす</title> <style> body { margin: 0; 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: 400, height: 600, backgroundColor: '#fdf6e3', physics: { default: 'matter', matter: { gravity: { y: 1 } }, }, scene: { create: function () { const box = { left: 60, right: 340, top: 150, bottom: 540, wall: 12 };
const g = this.add.graphics(); g.fillStyle(0xe74c3c, 1); g.fillRect(box.left - box.wall, box.top, box.wall, box.bottom - box.top);
g.fillStyle(0x2ecc71, 1); g.fillRect(box.right, box.top, box.wall, box.bottom - box.top);
g.fillStyle(0x3498db, 1); g.fillRect(box.left - box.wall, box.bottom, box.right - box.left + box.wall * 2, box.wall);
const wallH = box.bottom - box.top; const wallCY = (box.top + box.bottom) / 2; this.matter.add.rectangle(box.left - box.wall / 2, wallCY, box.wall, wallH, { isStatic: true }); this.matter.add.rectangle(box.right + box.wall / 2, wallCY, box.wall, wallH, { isStatic: true }); const floorW = box.right - box.left + box.wall * 2; this.matter.add.rectangle((box.left + box.right) / 2, box.bottom + box.wall / 2, floorW, box.wall, { isStatic: true });
this.input.on('pointerdown', (pointer) => { const radius = 25; const dropY = 100;
// 箱の内側からはみ出さないよう X を制限する const x = Phaser.Math.Clamp(pointer.x, box.left + radius, box.right - radius);
const ball = this.add.circle(x, dropY, radius, 0x333333);
this.matter.add.gameObject(ball, { shape: { type: 'circle', radius: radius }, restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる) }); }); } },});