UI e Text
Text
create() {
// Text basilare
const text = this.add.text(100, 100, 'Hello Phaser!', {
fontSize: '32px',
fill: '#ffffff'
});
// Con stile completo
const styledText = this.add.text(400, 300, 'Styled Text', {
fontFamily: 'Arial',
fontSize: '48px',
fill: '#ff0000',
stroke: '#000000',
strokeThickness: 4,
shadow: {
offsetX: 2,
offsetY: 2,
color: '#000',
blur: 5,
fill: true
}
});
// Centra testo
text.setOrigin(0.5);
}
Bitmap Text
Usa font bitmap per performance migliori:
preload() {
this.load.bitmapFont('arcade', 'assets/arcade.png', 'assets/arcade.xml');
}
create() {
const bitmapText = this.add.bitmapText(400, 300, 'arcade', 'SCORE: 0', 32);
bitmapText.setOrigin(0.5);
}
Buttons
create() {
// Button con sprite
const button = this.add.sprite(400, 300, 'button').setInteractive();
button.on('pointerover', () => {
button.setTint(0xcccccc);
});
button.on('pointerout', () => {
button.clearTint();
});
button.on('pointerdown', () => {
this.scene.start('GameScene');
});
// Text button
const textButton = this.add.text(400, 400, 'Start Game', {
fontSize: '32px',
fill: '#fff',
backgroundColor: '#000',
padding: { x: 20, y: 10 }
}).setOrigin(0.5).setInteractive();
textButton.on('pointerdown', () => {
this.scene.start('GameScene');
});
}
Health Bar
create() {
this.healthBar = this.add.graphics();
this.updateHealthBar(100);
}
updateHealthBar(health) {
this.healthBar.clear();
// Background
this.healthBar.fillStyle(0x000000);
this.healthBar.fillRect(10, 10, 204, 24);
// Border
this.healthBar.lineStyle(2, 0xffffff);
this.healthBar.strokeRect(10, 10, 204, 24);
// Health
const color = health > 50 ? 0x00ff00 : health > 25 ? 0xffff00 : 0xff0000;
this.healthBar.fillStyle(color);
this.healthBar.fillRect(12, 12, health * 2, 20);
}
Progress Bar
preload() {
// Progress bar durante loading
const progressBar = this.add.graphics();
const progressBox = this.add.graphics();
progressBox.fillStyle(0x222222, 0.8);
progressBox.fillRect(240, 270, 320, 50);
const width = this.cameras.main.width;
const height = this.cameras.main.height;
const loadingText = this.make.text({
x: width / 2,
y: height / 2 - 50,
text: 'Loading...',
style: {
font: '20px monospace',
fill: '#ffffff'
}
});
loadingText.setOrigin(0.5, 0.5);
this.load.on('progress', (value) => {
progressBar.clear();
progressBar.fillStyle(0xffffff, 1);
progressBar.fillRect(250, 280, 300 * value, 30);
});
this.load.on('complete', () => {
progressBar.destroy();
progressBox.destroy();
loadingText.destroy();
});
}
Score Display
create() {
this.score = 0;
this.scoreText = this.add.text(16, 16, 'Score: 0', {
fontSize: '32px',
fill: '#000'
});
// Fixed to camera
this.scoreText.setScrollFactor(0);
}
addScore(points) {
this.score += points;
this.scoreText.setText('Score: ' + this.score);
// Animate score
this.tweens.add({
targets: this.scoreText,
scaleX: 1.2,
scaleY: 1.2,
duration: 100,
yoyo: true
});
}
Timer Display
create() {
this.timeLeft = 60;
this.timerText = this.add.text(400, 16, 'Time: 60', {
fontSize: '32px',
fill: '#000'
}).setOrigin(0.5, 0);
this.time.addEvent({
delay: 1000,
callback: this.updateTimer,
callbackScope: this,
loop: true
});
}
updateTimer() {
this.timeLeft--;
this.timerText.setText('Time: ' + this.timeLeft);
if (this.timeLeft <= 10) {
this.timerText.setColor('#ff0000');
}
if (this.timeLeft <= 0) {
this.gameOver();
}
}
Menu
class MenuScene extends Phaser.Scene {
create() {
// Title
this.add.text(400, 100, 'MY GAME', {
fontSize: '64px',
fill: '#fff'
}).setOrigin(0.5);
// Menu options
const options = ['Start Game', 'Options', 'Credits', 'Exit'];
const startY = 250;
const spacing = 60;
options.forEach((option, index) => {
const text = this.add.text(400, startY + index * spacing, option, {
fontSize: '32px',
fill: '#fff'
}).setOrigin(0.5).setInteractive();
text.on('pointerover', () => {
text.setColor('#ffff00');
});
text.on('pointerout', () => {
text.setColor('#ffffff');
});
text.on('pointerdown', () => {
this.handleMenuClick(index);
});
});
}
handleMenuClick(index) {
switch(index) {
case 0: this.scene.start('GameScene'); break;
case 1: this.scene.start('OptionsScene'); break;
case 2: this.scene.start('CreditsScene'); break;
case 3: window.close(); break;
}
}
}
Dialog Box
showDialog(text) {
// Background
const box = this.add.graphics();
box.fillStyle(0x000000, 0.8);
box.fillRect(50, 400, 700, 150);
box.lineStyle(2, 0xffffff);
box.strokeRect(50, 400, 700, 150);
// Text
const dialogText = this.add.text(70, 420, '', {
fontSize: '24px',
fill: '#fff',
wordWrap: { width: 660 }
});
// Typewriter effect
let i = 0;
this.time.addEvent({
delay: 50,
callback: () => {
dialogText.text += text[i];
i++;
},
repeat: text.length - 1
});
// Continue indicator
const continueText = this.add.text(720, 530, '▼', {
fontSize: '24px',
fill: '#fff'
}).setOrigin(1);
this.tweens.add({
targets: continueText,
alpha: 0,
duration: 500,
yoyo: true,
repeat: -1
});
// Click to continue
this.input.once('pointerdown', () => {
box.destroy();
dialogText.destroy();
continueText.destroy();
});
}
Inventory Grid
create() {
const startX = 50;
const startY = 50;
const slotSize = 64;
const spacing = 10;
const cols = 5;
const rows = 3;
this.inventorySlots = [];
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const x = startX + col * (slotSize + spacing);
const y = startY + row * (slotSize + spacing);
// Slot background
const slot = this.add.rectangle(x, y, slotSize, slotSize, 0x222222)
.setOrigin(0)
.setInteractive();
// Border
const border = this.add.rectangle(x, y, slotSize, slotSize)
.setOrigin(0)
.setStrokeStyle(2, 0xffffff);
slot.on('pointerdown', () => {
this.selectSlot(row * cols + col);
});
this.inventorySlots.push({ slot, border, item: null });
}
}
}
addItemToInventory(itemKey) {
const emptySlot = this.inventorySlots.find(s => s.item === null);
if (emptySlot) {
emptySlot.item = this.add.image(
emptySlot.slot.x + 32,
emptySlot.slot.y + 32,
itemKey
).setDisplaySize(48, 48);
}
}
Pause Menu
create() {
this.isPaused = false;
// ESC to pause
this.input.keyboard.on('keydown-ESC', () => {
this.togglePause();
});
}
togglePause() {
this.isPaused = !this.isPaused;
if (this.isPaused) {
this.physics.pause();
this.showPauseMenu();
} else {
this.physics.resume();
this.hidePauseMenu();
}
}
showPauseMenu() {
// Overlay
this.pauseOverlay = this.add.rectangle(400, 300, 800, 600, 0x000000, 0.7);
this.pauseOverlay.setScrollFactor(0);
// Text
this.pauseText = this.add.text(400, 200, 'PAUSED', {
fontSize: '64px',
fill: '#fff'
}).setOrigin(0.5).setScrollFactor(0);
// Resume button
const resumeButton = this.add.text(400, 300, 'Resume', {
fontSize: '32px',
fill: '#fff'
}).setOrigin(0.5).setInteractive().setScrollFactor(0);
resumeButton.on('pointerdown', () => {
this.togglePause();
});
}
hidePauseMenu() {
this.pauseOverlay.destroy();
this.pauseText.destroy();
}
Best Practices
- Fixed UI: Usa
setScrollFactor(0)per UI fissa - Bitmap fonts: Migliori performance per text dinamico
- Interactive: Usa
setInteractive()per elementi cliccabili - Feedback visivo: Hover effects migliorano UX
- Responsive: Adatta UI a diverse risoluzioni
Conclusione
UI ben progettata è essenziale per un gioco professionale. Phaser offre tutti gli strumenti necessari per creare interfacce intuitive e attraenti.
