スタート画面を作る
前の 07 箱を積んで崩す まで、ゲームは1画面だけでした。この節では 「スタート画面」を作り、ボタンを押すとゲームが始まるようにします。そのために、画面ごとに シーン(Scene) という単位でコードを分けます。
今回さわる
game/:main.jsを書き換え(シーンを2つに分ける)
前の節を終えていない人は、ここから始められます。
シーンという考え方
Section titled “シーンという考え方”Phaser では、タイトル画面・ゲーム本編・結果画面などを シーン という単位で分けて作れます。 この節では2つのシーンを用意します。
StartScene… タイトルとスタートボタンを出す画面。GameScene… これまで作ってきたゲーム本編(パチンコ・箱)。
スタート画面のシーン
Section titled “スタート画面のシーン”タイトルとボタンを出す StartScene を書きます。ボタンを押すと GameScene へ切り替えます。
続けて、ゲーム本編を GameScene に移し、new Phaser.Game の scene に2つのシーンを登録します(下の見出しで説明します)。
差分の最後で、前の節の scene: { create: ... } が消え、そこにあったコードは GameScene の create に入っています。
main.js を変更する
Section titled “main.js を変更する”1new Phaser.Game({2 type: Phaser.AUTO,3 width: 720,4 height: 480,5 backgroundColor: '#fdf6e3',6 physics: {7 default: 'matter',8 matter: { gravity: { y: 1 } },9 },10 scene: {11 create: function () {12 const groundX = 360;13 const groundY = 440;14 const groundWidth = 720;15 const groundHeight = 80;16 const groundTop = groundY - groundHeight / 2; // 地面の上面。物はこの高さに乗る1class StartScene extends Phaser.Scene {2 constructor() {3 super('Start');4 }175
18 const ground = this.add.rectangle(19 groundX,20 groundY,21 groundWidth,22 groundHeight,23 0x888888,24 );6 create() {7 this.add8 .text(360, 180, 'パチンコ物理ゲーム', {9 fontSize: '36px',10 color: '#5d4037',11 fontStyle: 'bold',12 })13 .setOrigin(0.5);2514
26 this.matter.add.gameObject(ground, { isStatic: true });15 const startText = this.add16 .text(360, 300, '▶ スタート', {17 fontSize: '28px',18 color: '#ffffff',19 backgroundColor: '#e08e3c',20 padding: { x: 24, y: 12 },21 })22 .setOrigin(0.5)23 .setInteractive({ useHandCursor: true });2724
28 const boxSize = 40;29 const towerX = 560;30 for (let i = 0; i < 3; i++) {31 const boxY = groundTop - boxSize / 2 - i * boxSize;32 const box = this.add.rectangle(33 towerX,34 boxY,35 boxSize,36 boxSize,37 0xdddddd,38 );39 box.setStrokeStyle(3, 0x333333);40 this.matter.add.gameObject(box, { restitution: 0.1 });41 }25 startText.on('pointerdown', () => this.scene.start('Game'));26 }27}4228
43 const anchor = { x: 140, y: 300 };44 const power = 0.22; // 引っ張った長さを速さに変える倍率29class GameScene extends Phaser.Scene {30 constructor() {31 super('Game');32 }4533
46 this.add.circle(anchor.x, anchor.y, 6, 0xbbbbbb);34 create() {35 const groundX = 360;36 const groundY = 440;37 const groundWidth = 720;38 const groundHeight = 80;39 const groundTop = groundY - groundHeight / 2; // 地面の上面。物はこの高さに乗る4740
48 const aim = this.add.graphics();41 const ground = this.add.rectangle(42 groundX,43 groundY,44 groundWidth,45 groundHeight,46 0x888888,47 );4948
50 const birdRadius = 18;51 const bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);52 bird.setStrokeStyle(3, 0x333333);49 this.matter.add.gameObject(ground, { isStatic: true });5350
54 this.matter.add.gameObject(bird, {55 shape: {56 type: 'circle',57 radius: birdRadius,58 },59 restitution: 0.2,60 });51 const boxSize = 40;52 const towerX = 560;53 for (let i = 0; i < 3; i++) {54 const boxY = groundTop - boxSize / 2 - i * boxSize;55 const box = this.add.rectangle(towerX, boxY, boxSize, boxSize, 0xdddddd);56 box.setStrokeStyle(3, 0x333333);57 this.matter.add.gameObject(box, { restitution: 0.1 });58 }6159
62 // 待機中は動かないように静的にしておく。63 bird.setStatic(true);60 const anchor = { x: 140, y: 300 };61 const power = 0.22; // 引っ張った長さを速さに変える倍率6462
65 let dragging = false;63 this.add.circle(anchor.x, anchor.y, 6, 0xbbbbbb);6664
67 this.input.on('pointerdown', function () {68 bird.setStatic(true);69 bird.setPosition(anchor.x, anchor.y);70 bird.setVelocity(0, 0);71 dragging = true;72 });65 const aim = this.add.graphics();7366
74 this.input.on('pointermove', function (pointer) {75 if (!dragging) return;67 const birdRadius = 18;68 const bird = this.add.circle(anchor.x, anchor.y, birdRadius, 0xffffff);69 bird.setStrokeStyle(3, 0x333333);7670
77 bird.setPosition(pointer.x, pointer.y);71 this.matter.add.gameObject(bird, {72 shape: {73 type: 'circle',74 radius: birdRadius,75 },76 restitution: 0.2,77 });7878
79 const forwardX = anchor.x + (anchor.x - bird.x) * 1.5;80 const forwardY = anchor.y + (anchor.y - bird.y) * 1.5;81 aim.clear();82 aim.lineStyle(2, 0x333333, 0.5);83 aim.lineBetween(bird.x, bird.y, forwardX, forwardY);84 });79 // 待機中は動かないように静的にしておく。80 bird.setStatic(true);8581
86 this.input.on('pointerup', function () {87 if (!dragging) return;88 dragging = false;82 let dragging = false;8983
90 aim.clear();84 this.input.on('pointerdown', () => {85 bird.setStatic(true);86 bird.setPosition(anchor.x, anchor.y);87 bird.setVelocity(0, 0);88 dragging = true;89 });9190
92 const vx = (anchor.x - bird.x) * power;93 const vy = (anchor.y - bird.y) * power;91 this.input.on('pointermove', (pointer) => {92 if (!dragging) return;9493
95 bird.setStatic(false);96 bird.setVelocity(vx, vy);97 });98 },94 bird.setPosition(pointer.x, pointer.y);95
96 const forwardX = anchor.x + (anchor.x - bird.x) * 1.5;97 const forwardY = anchor.y + (anchor.y - bird.y) * 1.5;98 aim.clear();99 aim.lineStyle(2, 0x333333, 0.5);100 aim.lineBetween(bird.x, bird.y, forwardX, forwardY);101 });102
103 this.input.on('pointerup', () => {104 if (!dragging) return;105 dragging = false;106
107 aim.clear();108
109 const vx = (anchor.x - bird.x) * power;110 const vy = (anchor.y - bird.y) * power;111
112 bird.setStatic(false);113 bird.setVelocity(vx, vy);114 });115 }116}117
118new Phaser.Game({119 type: Phaser.AUTO,120 width: 720,121 height: 480,122 backgroundColor: '#fdf6e3',123 physics: {124 default: 'matter',125 matter: { gravity: { y: 1 } },99126 },127 scene: [StartScene, GameScene],100128});class StartScene extends Phaser.Scene… 1つのシーンを「クラス」として定義します。super('Start')… このシーンに'Start'という名前をつけます。切り替えるときにこの名前を使います。this.add.text(x, y, '文字', { ... })… 文字を置きます。setOrigin(0.5)で文字の中心を指定位置に合わせます。setInteractive({ useHandCursor: true })… 押せるようにし、カーソルを指の形にします。startText.on('pointerdown', () => this.scene.start('Game'))… ボタンが押されたら'Game'シーンへ切り替えます。
ゲーム本編のシーン
Section titled “ゲーム本編のシーン”これまで create に書いていたゲーム本編を、GameScene クラスの create の中へそのまま移します。
super('Game')… このシーンの名前は'Game'。StartSceneからthis.scene.start('Game')で呼ばれます。- 入力処理の書き方が少し変わります。
function () { ... }の代わりに() => { ... }(アロー関数)を使うと、 中のthisがシーンを指したままになるので扱いやすくなります。
シーンを登録して起動する
Section titled “シーンを登録して起動する”最後に、new Phaser.Game の scene に2つのシーンを順番に渡します。最初のものが最初に表示されます。
最初にタイトルとスタートボタンが出ます。ボタンを押すとゲームが始まります。 次の節で、鳥を撃つたびに次の鳥がセットされる「リロード」を作ります。