| name | phaser-scene-transition-patterns |
| description | Common Phaser scene transition patterns, data passing, and test seam integration for scene management. Use when implementing scene navigation, passing data between scenes, or integrating test seams for scene transitions. Reduces scene transition implementation time by 30-40%. |
Phaser Scene Transition Patterns
Common Phaser scene transition patterns, data passing, and test seam integration. Reduces scene transition implementation time by 30-40%.
Overview
Standardized patterns for:
- Scene navigation (
this.scene.start())
- Data passing between scenes
- Test seam integration for transitions
- State management on transitions
Core Patterns
Pattern 1: Scene Navigation
Use this.scene.start() for transitions:
this.scene.start('GameScene');
this.scene.start('GameOverScene', {
score: this.gameState.score,
level: this.gameState.level
});
Each scene receives data via init(data?) method:
export default class GameOverScene extends Phaser.Scene {
init(data?: { score?: number; level?: number }) {
this.finalScore = data?.score || 0;
this.completedLevel = data?.level || 1;
}
create() {
this.add.text(100, 100, `Score: ${this.finalScore}`);
}
}
Pattern 2: Data Passing
Pass data via scene.start() second parameter:
gameOver() {
this.scene.start('GameOverScene', {
score: this.gameState.score,
timer: this.gameState.timer,
level: this.gameState.level
});
}
Store in shared state object for test seam access:
this.registry.set('gameState', {
score: this.gameState.score,
level: this.gameState.level
});
const gameState = this.registry.get('gameState');
Use Phaser's scene data mechanism for reliability:
this.scene.start('NextScene', { key: 'value' });
init(data?: { key?: string }) {
const value = data?.key;
}
Pattern 3: Test Seam Integration
Add test seam in scene create() method:
create() {
this.initScene();
if (window.__TEST__) {
window.__TEST__.sceneKey = this.scene.key;
window.__TEST__.commands.goToScene = (key: string, data?: any) => {
this.scene.start(key, data);
};
}
}
Expose sceneKey, commands, and state getters:
window.__TEST__ = {
sceneKey: this.scene.key,
gameState: () => ({
scene: this.scene.key,
score: this.gameState.score,
timer: this.gameState.timer
}),
commands: {
goToScene: (key: string, data?: any) => {
this.scene.start(key, data);
}
}
};
Wait for test seam initialization after transitions:
agent-browser eval "window.__TEST__.commands.goToScene('GameScene')"
agent-browser wait 500
agent-browser eval "window.__TEST__.sceneKey"
Pattern 4: State Management
Game state clears when transitioning to fresh scenes:
create() {
this.gameState = {
score: 0,
timer: 60,
level: 1
};
}
Test seam enables programmatic state verification:
window.__TEST__.gameState = () => ({
scene: this.scene.key,
score: this.gameState.score,
timer: this.gameState.timer,
level: this.gameState.level
});
Complete Scene Transition Example
GameScene → GameOverScene
In GameScene:
export default class GameScene extends Phaser.Scene {
private gameState = {
score: 0,
timer: 60,
level: 1
};
gameOver() {
this.scene.start('GameOverScene', {
score: this.gameState.score,
timer: this.gameState.timer,
level: this.gameState.level
});
}
create() {
if (window.__TEST__) {
window.__TEST__.sceneKey = this.scene.key;
window.__TEST__.commands.triggerGameOver = () => {
this.gameOver();
};
}
}
}
In GameOverScene:
export default class GameOverScene extends Phaser.Scene {
private finalScore = 0;
private completedLevel = 1;
init(data?: { score?: number; level?: number }) {
this.finalScore = data?.score || 0;
this.completedLevel = data?.level || 1;
}
create() {
this.add.text(100, 100, `Final Score: ${this.finalScore}`);
if (window.__TEST__) {
window.__TEST__.sceneKey = this.scene.key;
window.__TEST__.commands.getFinalScore = () => {
return this.finalScore;
};
}
}
}
Test Seam Commands for Scene Transitions
Common Commands
window.__TEST__.commands.goToScene = (key: string, data?: any) => {
this.scene.start(key, data);
};
window.__TEST__.commands.clickStartGame = () => {
this.scene.start('GameScene');
};
window.__TEST__.commands.clickMainMenu = () => {
this.scene.start('MainMenuScene');
};
window.__TEST__.commands.clickPlayAgain = () => {
this.scene.start('GameScene');
};
Scene Lifecycle
Scene Initialization Order
- Constructor: Configuration constants
- init(data?): Receive data from previous scene
- preload(): Load assets (if needed)
- create(): Initialize scene, set up test seam
- update(): Game loop
State Clearing
Each scene initializes fresh state in create():
create() {
this.gameState = {
score: 0,
timer: 60
};
}
Common Patterns
Pattern 1: MainMenu → GameScene
create() {
this.startButton.on('pointerdown', () => {
this.scene.start('GameScene');
});
window.__TEST__.commands.clickStartGame = () => {
this.scene.start('GameScene');
};
}
Pattern 2: GameScene → GameOverScene (with data)
gameOver() {
this.scene.start('GameOverScene', {
score: this.gameState.score,
level: this.gameState.level
});
}
init(data?: { score?: number; level?: number }) {
this.finalScore = data?.score || 0;
this.completedLevel = data?.level || 1;
}
Pattern 3: Any Scene → MainMenu
window.__TEST__.commands.clickMainMenu = () => {
this.scene.start('MainMenuScene');
};
Testing Scene Transitions
Test Seam Workflow
agent-browser eval "window.__TEST__.commands.goToScene('GameScene')"
agent-browser wait 500
agent-browser eval "window.__TEST__.sceneKey === 'GameScene'"
agent-browser eval "window.__TEST__.gameState()"
Console Log Fallback
If test seam sceneKey doesn't update, use console logs:
create() {
console.log(`Scene created: ${this.scene.key}`);
}
Verify in browser console:
agent-browser console
Best Practices
- Use
this.scene.start() for scene transitions
- Pass data via second parameter of scene.start()
- Receive data in
init(data?) method
- Set up test seam in
create() method
- Initialize fresh state in each scene's create()
- Wait 500ms after transitions before checking test seam
- Use console logs as fallback for scene verification
Integration with Other Skills
- phaser-scene-navigation: Complements with navigation patterns
- phaser-game-testing: Uses test seam for scene testing
- browser-test-seam-optimization: Optimizes scene transition testing
Related Skills
phaser-scene-navigation - Scene navigation patterns
phaser-game-testing - Phaser testing methodology
browser-test-seam-optimization - Test seam optimization
Remember
- Use scene.start() for transitions
- Pass data via second parameter
- Receive in init() method
- Set up test seam in create()
- Initialize fresh state per scene
- Wait 500ms after transitions
- Use console logs as fallback