export class Button extends Phaser.GameObjects.Container { private buttonImage: Phaser.GameObjects.Image; private buttonText: Phaser.GameObjects.Text; constructor( scene: Phaser.Scene, x: number, y: number, texture: string, text: string, callback: () => void ) { super(scene, x, y); // 1. 画像の追加 this.buttonImage = scene.add.image(0, 0, texture); // 2. テキストの追加 this.buttonText = scene.add.text(0, 0, text, { fontSize: '24px', color: '#ffffff' }).setOrigin(0.5); // Containerに追加 this.add([this.buttonImage, this.buttonText]); // 3. インタラクティブ設定(クリック可能にする) this.setSize(this.buttonImage.width, this.buttonImage.height); this.setInteractive({ useHandCursor: true }) .on('pointerover', () => this.onHover()) .on('pointerout', () => this.onRest()) .on('pointerdown', () => this.onDown()) .on('pointerup', () => { this.onHover(); callback(); }); // シーンに追加 scene.add.existing(this); } private onHover() { this.buttonImage.setTint(0xdddddd); this.setScale(1.05); } private onRest() { this.buttonImage.clearTint(); this.setScale(1.0); } private onDown() { this.buttonImage.setTint(0xbbbbbb); this.setScale(0.95); } } ///////////// import { Button } from './Button'; export class MainScene extends Phaser.Scene { create() { // ボタンの生成 const startButton = new Button( this, 400, 300, 'button-texture', 'START', () => { this.scene.start('GameScene'); // シーン遷移などの処理 } ); } }
bbtest