├── src ├── vite-env.d.ts ├── game │ ├── EventBus.ts │ ├── scenes │ │ ├── Boot.ts │ │ ├── GameOver.ts │ │ ├── Game.ts │ │ ├── Preloader.ts │ │ └── MainMenu.ts │ └── main.ts ├── index.tsx ├── PhaserGame.tsx └── App.tsx ├── screenshot.png ├── public ├── favicon.png ├── assets │ ├── bg.png │ ├── logo.png │ └── star.png └── style.css ├── vite ├── config.dev.mjs └── config.prod.mjs ├── .editorconfig ├── tsconfig.node.json ├── .gitignore ├── index.html ├── tsconfig.json ├── log.js ├── LICENSE ├── package.json └── README.md /src/vite-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phaserjs/template-solid/HEAD/screenshot.png -------------------------------------------------------------------------------- /public/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phaserjs/template-solid/HEAD/public/favicon.png -------------------------------------------------------------------------------- /public/assets/bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phaserjs/template-solid/HEAD/public/assets/bg.png -------------------------------------------------------------------------------- /public/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phaserjs/template-solid/HEAD/public/assets/logo.png -------------------------------------------------------------------------------- /public/assets/star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phaserjs/template-solid/HEAD/public/assets/star.png -------------------------------------------------------------------------------- /src/game/EventBus.ts: -------------------------------------------------------------------------------- 1 | import { Events } from 'phaser'; 2 | 3 | // Used to emit events between components, HTML and Phaser scenes 4 | export const EventBus = new Events.EventEmitter(); -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | /* @refresh reload */ 2 | import { render } from 'solid-js/web'; 3 | 4 | import App from './App'; 5 | 6 | const root = document.getElementById('root'); 7 | 8 | render(() => , root!); 9 | -------------------------------------------------------------------------------- /vite/config.dev.mjs: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | import solid from 'vite-plugin-solid'; 3 | 4 | // https://vitejs.dev/config/ 5 | export default defineConfig({ 6 | plugins: [ 7 | solid(), 8 | ], 9 | server: { 10 | port: 8080 11 | } 12 | }) 13 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: https://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | [*] 7 | indent_style = space 8 | indent_size = 4 9 | end_of_line = crlf 10 | charset = utf-8 11 | trim_trailing_whitespace = false 12 | insert_final_newline = false -------------------------------------------------------------------------------- /tsconfig.node.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "composite": true, 4 | "skipLibCheck": true, 5 | "module": "ESNext", 6 | "moduleResolution": "bundler", 7 | "allowSyntheticDefaultImports": true, 8 | "strict": true 9 | }, 10 | "include": ["vite.config.ts"] 11 | } 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | pnpm-debug.log* 8 | lerna-debug.log* 9 | 10 | node_modules 11 | dist 12 | dist-ssr 13 | *.local 14 | 15 | # Editor directories and files 16 | .vscode/* 17 | !.vscode/extensions.json 18 | .idea 19 | .DS_Store 20 | *.suo 21 | *.ntvs* 22 | *.njsproj 23 | *.sln 24 | *.sw? 25 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Phaser Solid Template 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/game/scenes/Boot.ts: -------------------------------------------------------------------------------- 1 | import { Scene } from 'phaser'; 2 | 3 | export class Boot extends Scene 4 | { 5 | constructor () 6 | { 7 | super('Boot'); 8 | } 9 | 10 | preload () 11 | { 12 | // The Boot Scene is typically used to load in any assets you require for your Preloader, such as a game logo or background. 13 | // The smaller the file size of the assets, the better, as the Boot Scene itself has no preloader. 14 | 15 | this.load.image('background', 'assets/bg.png'); 16 | } 17 | 18 | create () 19 | { 20 | this.scene.start('Preloader'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES2020", 4 | "useDefineForClassFields": true, 5 | "module": "ESNext", 6 | "lib": ["ES2020", "DOM", "DOM.Iterable"], 7 | "skipLibCheck": true, 8 | 9 | /* Bundler mode */ 10 | "moduleResolution": "bundler", 11 | "allowImportingTsExtensions": true, 12 | "resolveJsonModule": true, 13 | "isolatedModules": true, 14 | "noEmit": true, 15 | "jsx": "preserve", 16 | "jsxImportSource": "solid-js", 17 | 18 | /* Linting */ 19 | "strict": true, 20 | "noUnusedLocals": true, 21 | "strictPropertyInitialization": false, 22 | "noUnusedParameters": true, 23 | "noFallthroughCasesInSwitch": true 24 | }, 25 | "include": ["src"], 26 | "references": [{ "path": "./tsconfig.node.json" }] 27 | } 28 | -------------------------------------------------------------------------------- /vite/config.prod.mjs: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | import solid from 'vite-plugin-solid'; 3 | 4 | process.stdout.write(`Building for production...\n`); 5 | const line = "---------------------------------------------------------"; 6 | const msg = `❤️❤️❤️ Tell us about your game! - games@phaser.io ❤️❤️❤️`; 7 | process.stdout.write(`${line}\n${msg}\n${line}\n`); 8 | 9 | export default defineConfig({ 10 | base: './', 11 | plugins: [ 12 | solid(), 13 | ], 14 | logLevel: 'error', 15 | build: { 16 | minify: 'terser', 17 | terserOptions: { 18 | compress: { 19 | passes: 2 20 | }, 21 | mangle: true, 22 | format: { 23 | comments: false 24 | } 25 | } 26 | } 27 | }); 28 | 29 | -------------------------------------------------------------------------------- /src/game/main.ts: -------------------------------------------------------------------------------- 1 | import { Boot } from './scenes/Boot'; 2 | import { GameOver } from './scenes/GameOver'; 3 | import { Game as MainGame } from './scenes/Game'; 4 | import { MainMenu } from './scenes/MainMenu'; 5 | import { AUTO, Game } from 'phaser'; 6 | import { Preloader } from './scenes/Preloader'; 7 | 8 | // Find out more information about the Game Config at: 9 | // https://docs.phaser.io/api-documentation/typedef/types-core#gameconfig 10 | const config: Phaser.Types.Core.GameConfig = { 11 | type: AUTO, 12 | width: 1024, 13 | height: 768, 14 | parent: 'game-container', 15 | backgroundColor: '#028af8', 16 | scene: [ 17 | Boot, 18 | Preloader, 19 | MainMenu, 20 | MainGame, 21 | GameOver 22 | ] 23 | }; 24 | 25 | const StartGame = (parent: string) => { 26 | 27 | return new Game({ ...config, parent }); 28 | 29 | } 30 | 31 | export default StartGame; 32 | -------------------------------------------------------------------------------- /log.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const https = require('https'); 3 | 4 | const main = async () => { 5 | const args = process.argv.slice(2); 6 | const packageData = JSON.parse(fs.readFileSync('./package.json', 'utf8')); 7 | const event = args[0] || 'unknown'; 8 | const phaserVersion = packageData.dependencies.phaser; 9 | 10 | const options = { 11 | hostname: 'gryzor.co', 12 | port: 443, 13 | path: `/v/${event}/${phaserVersion}/${packageData.name}`, 14 | method: 'GET' 15 | }; 16 | 17 | try { 18 | const req = https.request(options, (res) => { 19 | res.on('data', () => {}); 20 | res.on('end', () => { 21 | process.exit(0); 22 | }); 23 | }); 24 | 25 | req.on('error', (error) => { 26 | process.exit(1); 27 | }); 28 | 29 | req.end(); 30 | } catch (error) { 31 | // Silence is the canvas where the soul paints its most profound thoughts. 32 | process.exit(1); 33 | } 34 | } 35 | 36 | main(); 37 | -------------------------------------------------------------------------------- /src/game/scenes/GameOver.ts: -------------------------------------------------------------------------------- 1 | import { EventBus } from '../EventBus'; 2 | import { Scene } from 'phaser'; 3 | 4 | export class GameOver extends Scene 5 | { 6 | camera: Phaser.Cameras.Scene2D.Camera; 7 | background: Phaser.GameObjects.Image; 8 | gameOverText : Phaser.GameObjects.Text; 9 | 10 | constructor () 11 | { 12 | super('GameOver'); 13 | } 14 | 15 | create () 16 | { 17 | this.camera = this.cameras.main 18 | this.camera.setBackgroundColor(0xff0000); 19 | 20 | this.background = this.add.image(512, 384, 'background'); 21 | this.background.setAlpha(0.5); 22 | 23 | this.gameOverText = this.add.text(512, 384, 'Game Over', { 24 | fontFamily: 'Arial Black', fontSize: 64, color: '#ffffff', 25 | stroke: '#000000', strokeThickness: 8, 26 | align: 'center' 27 | }).setOrigin(0.5).setDepth(100); 28 | 29 | EventBus.emit('current-scene-ready', this); 30 | } 31 | 32 | changeScene () 33 | { 34 | this.scene.start('MainMenu'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/game/scenes/Game.ts: -------------------------------------------------------------------------------- 1 | import { EventBus } from '../EventBus'; 2 | import { Scene } from 'phaser'; 3 | 4 | export class Game extends Scene 5 | { 6 | camera: Phaser.Cameras.Scene2D.Camera; 7 | background: Phaser.GameObjects.Image; 8 | gameText: Phaser.GameObjects.Text; 9 | 10 | constructor () 11 | { 12 | super('Game'); 13 | } 14 | 15 | create () 16 | { 17 | this.camera = this.cameras.main; 18 | this.camera.setBackgroundColor(0x00ff00); 19 | 20 | this.background = this.add.image(512, 384, 'background'); 21 | this.background.setAlpha(0.5); 22 | 23 | this.gameText = this.add.text(512, 384, 'Make something fun!\nand share it with us:\nsupport@phaser.io', { 24 | fontFamily: 'Arial Black', fontSize: 38, color: '#ffffff', 25 | stroke: '#000000', strokeThickness: 8, 26 | align: 'center' 27 | }).setOrigin(0.5).setDepth(100); 28 | 29 | EventBus.emit('current-scene-ready', this); 30 | } 31 | 32 | changeScene () 33 | { 34 | this.scene.start('GameOver'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /public/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | color: rgba(255, 255, 255, 0.87); 5 | background-color: #000000; 6 | font-family: Arial, Helvetica, sans-serif; 7 | } 8 | 9 | #app { 10 | width: 100%; 11 | height: 100vh; 12 | overflow: hidden; 13 | display: flex; 14 | justify-content: center; 15 | align-items: center; 16 | } 17 | 18 | .spritePosition { 19 | margin: 10px 0 0 10px; 20 | font-size: 0.8em; 21 | } 22 | 23 | .button { 24 | width: 140px; 25 | margin: 10px; 26 | padding: 10px; 27 | background-color: #000000; 28 | color: rgba(255, 255, 255, 0.87); 29 | border: 1px solid rgba(255, 255, 255, 0.87); 30 | cursor: pointer; 31 | transition: all 0.3s; 32 | 33 | &:hover { 34 | border: 1px solid #0ec3c9; 35 | color: #0ec3c9; 36 | } 37 | 38 | &:active { 39 | background-color: #0ec3c9; 40 | } 41 | 42 | /* Disabled styles */ 43 | &:disabled { 44 | cursor: not-allowed; 45 | border: 1px solid rgba(255, 255, 255, 0.3); 46 | color: rgba(255, 255, 255, 0.3); 47 | } 48 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Phaser Studio Inc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "template-solidjs", 3 | "description": "A Phaser 3 template using SolidJS.", 4 | "version": "1.2.0", 5 | "repository": { 6 | "type": "git", 7 | "url": "git+https://github.com/phaserjs/template-solid.git" 8 | }, 9 | "author": "Phaser Studio (https://phaser.io/)", 10 | "license": "MIT", 11 | "licenseUrl": "http://www.opensource.org/licenses/mit-license.php", 12 | "bugs": { 13 | "url": "https://github.com/phaserjs/template-solid/issues" 14 | }, 15 | "homepage": "https://github.com/phaserjs/template-solid#readme", 16 | "keywords": [ 17 | "phaser", 18 | "phaser3", 19 | "solidjs", 20 | "vite", 21 | "typescript" 22 | ], 23 | "scripts": { 24 | "dev": "node log.js dev & vite --config vite/config.dev.mjs", 25 | "build": "node log.js build & vite build --config vite/config.prod.mjs", 26 | "dev-nolog": "vite --config vite/config.dev.mjs", 27 | "build-nolog": "vite build --config vite/config.prod.mjs" 28 | }, 29 | "dependencies": { 30 | "phaser": "^3.90.0", 31 | "solid-js": "1.9.5" 32 | }, 33 | "devDependencies": { 34 | "terser": "5.39.0", 35 | "typescript": "5.7.2", 36 | "vite": "6.3.1", 37 | "vite-plugin-solid": "2.11.6" 38 | } 39 | } -------------------------------------------------------------------------------- /src/game/scenes/Preloader.ts: -------------------------------------------------------------------------------- 1 | import { Scene } from 'phaser'; 2 | 3 | export class Preloader extends Scene 4 | { 5 | constructor () 6 | { 7 | super('Preloader'); 8 | } 9 | 10 | init () 11 | { 12 | // We loaded this image in our Boot Scene, so we can display it here 13 | this.add.image(512, 384, 'background'); 14 | 15 | // A simple progress bar. This is the outline of the bar. 16 | this.add.rectangle(512, 384, 468, 32).setStrokeStyle(1, 0xffffff); 17 | 18 | // This is the progress bar itself. It will increase in size from the left based on the % of progress. 19 | const bar = this.add.rectangle(512-230, 384, 4, 28, 0xffffff); 20 | 21 | // Use the 'progress' event emitted by the LoaderPlugin to update the loading bar 22 | this.load.on('progress', (progress: number) => { 23 | 24 | // Update the progress bar (our bar is 464px wide, so 100% = 464px) 25 | bar.width = 4 + (460 * progress); 26 | 27 | }); 28 | } 29 | 30 | preload () 31 | { 32 | // Load the assets for the game - Replace with your own assets 33 | this.load.setPath('assets'); 34 | 35 | this.load.image('logo', 'logo.png'); 36 | this.load.image('star', 'star.png'); 37 | } 38 | 39 | create () 40 | { 41 | // When all the assets have loaded, it's often worth creating global objects here that the rest of the game can use. 42 | // For example, you can define global animations here, so we can use them in other scenes. 43 | 44 | // Move to the MainMenu. You could also swap this for a Scene Transition, such as a camera fade. 45 | this.scene.start('MainMenu'); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/PhaserGame.tsx: -------------------------------------------------------------------------------- 1 | import { onCleanup, onMount } from 'solid-js'; 2 | import { createStore } from 'solid-js/store'; 3 | import StartGame from './game/main'; 4 | import { EventBus } from './game/EventBus'; 5 | 6 | export interface IRefPhaserGame { 7 | game: Phaser.Game | null; 8 | scene: Phaser.Scene | null; 9 | } 10 | 11 | interface IProps { 12 | currentActiveScene?: (scene_instance: Phaser.Scene) => void; 13 | ref?: (instance: IRefPhaserGame) => void; // Optional ref callback prop 14 | } 15 | 16 | export const PhaserGame = (props: IProps) => { 17 | 18 | let gameContainer: HTMLDivElement | undefined; 19 | const [instance, setInstance] = createStore({ game: null, scene: null }); 20 | 21 | onMount(() => { 22 | const gameInstance = StartGame("game-container"); 23 | setInstance("game", gameInstance); 24 | 25 | if (props.ref) 26 | { 27 | props.ref({ game: gameInstance, scene: null }); 28 | } 29 | 30 | EventBus.on('current-scene-ready', (scene_instance: Phaser.Scene) => { 31 | 32 | if (props.currentActiveScene) 33 | { 34 | props.currentActiveScene(scene_instance); 35 | setInstance("scene", scene_instance); 36 | 37 | } 38 | 39 | if (props.ref) 40 | { 41 | props.ref({ game: gameInstance, scene: scene_instance }); 42 | } 43 | 44 | }); 45 | 46 | onCleanup(() => { 47 | 48 | if (instance.game) 49 | { 50 | instance.game.destroy(true); 51 | setInstance({ game: null, scene: null }); 52 | } 53 | 54 | EventBus.removeListener('current-scene-ready'); 55 | 56 | }); 57 | }); 58 | 59 | return ( 60 |
61 | ); 62 | }; -------------------------------------------------------------------------------- /src/game/scenes/MainMenu.ts: -------------------------------------------------------------------------------- 1 | import { GameObjects, Scene } from 'phaser'; 2 | 3 | import { EventBus } from '../EventBus'; 4 | 5 | export class MainMenu extends Scene 6 | { 7 | background: GameObjects.Image; 8 | logo: GameObjects.Image; 9 | title: GameObjects.Text; 10 | logoTween: Phaser.Tweens.Tween | null; 11 | 12 | constructor () 13 | { 14 | super('MainMenu'); 15 | } 16 | 17 | create () 18 | { 19 | this.background = this.add.image(512, 384, 'background'); 20 | 21 | this.logo = this.add.image(512, 300, 'logo').setDepth(100); 22 | 23 | this.title = this.add.text(512, 460, 'Main Menu', { 24 | fontFamily: 'Arial Black', fontSize: 38, color: '#ffffff', 25 | stroke: '#000000', strokeThickness: 8, 26 | align: 'center' 27 | }).setOrigin(0.5).setDepth(100); 28 | 29 | EventBus.emit('current-scene-ready', this); 30 | } 31 | 32 | changeScene () 33 | { 34 | if (this.logoTween) 35 | { 36 | this.logoTween.stop(); 37 | this.logoTween = null; 38 | } 39 | 40 | this.scene.start('Game'); 41 | } 42 | 43 | moveLogo (vueCallback: ({ x, y }: { x: number, y: number }) => void) 44 | { 45 | if (this.logoTween) 46 | { 47 | if (this.logoTween.isPlaying()) 48 | { 49 | this.logoTween.pause(); 50 | } 51 | else 52 | { 53 | this.logoTween.play(); 54 | } 55 | } 56 | else 57 | { 58 | this.logoTween = this.tweens.add({ 59 | targets: this.logo, 60 | x: { value: 750, duration: 3000, ease: 'Back.easeInOut' }, 61 | y: { value: 80, duration: 1500, ease: 'Sine.easeOut' }, 62 | yoyo: true, 63 | repeat: -1, 64 | onUpdate: () => { 65 | if (vueCallback) 66 | { 67 | vueCallback({ 68 | x: Math.floor(this.logo.x), 69 | y: Math.floor(this.logo.y) 70 | }); 71 | } 72 | } 73 | }); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/App.tsx: -------------------------------------------------------------------------------- 1 | import { createSignal } from 'solid-js'; 2 | import type { IRefPhaserGame } from './PhaserGame'; 3 | import { PhaserGame } from './PhaserGame'; 4 | import Phaser from 'phaser'; 5 | import { MainMenu } from './game/scenes/MainMenu'; 6 | 7 | const App = () => { 8 | 9 | // The sprite can only be moved in the MainMenu Scene 10 | const [canMoveSprite, setCanMoveSprite] = createSignal(true); 11 | 12 | // References to the PhaserGame component (game and scene are exposed) 13 | let phaserRef: IRefPhaserGame; 14 | const [spritePosition, setSpritePosition] = createSignal({ x: 0, y: 0 }); 15 | 16 | const changeScene = () => { 17 | 18 | const scene = phaserRef.scene as MainMenu; 19 | 20 | if (scene) 21 | { 22 | scene.changeScene(); 23 | } 24 | 25 | } 26 | 27 | const moveSprite = () => { 28 | 29 | const scene = phaserRef.scene as MainMenu; 30 | 31 | if (scene) 32 | { 33 | if (scene.scene.key === 'MainMenu') 34 | { 35 | // Get the update logo position 36 | scene.moveLogo(({ x, y }) => { 37 | 38 | setSpritePosition({ x, y }); 39 | 40 | }); 41 | } 42 | } 43 | } 44 | 45 | const addSprite = () => { 46 | 47 | const scene = phaserRef.scene; 48 | 49 | if (scene) 50 | { 51 | // Add more stars 52 | const x = Phaser.Math.Between(64, scene.scale.width - 64); 53 | const y = Phaser.Math.Between(64, scene.scale.height - 64); 54 | 55 | // `add.sprite` is a Phaser GameObjectFactory method and it returns a Sprite Game Object instance 56 | const star = scene.add.sprite(x, y, 'star'); 57 | 58 | // ... which you can then act upon. Here we create a Phaser Tween to fade the star sprite in and out. 59 | // You could, of course, do this from within the Phaser Scene code, but this is just an example 60 | // showing that Phaser objects and systems can be acted upon from outside of Phaser itself. 61 | scene.add.tween({ 62 | targets: star, 63 | duration: 500 + Math.random() * 1000, 64 | alpha: 0, 65 | yoyo: true, 66 | repeat: -1 67 | }); 68 | } 69 | 70 | } 71 | 72 | // Event emitted from the PhaserGame component 73 | const currentScene = (scene: Phaser.Scene) => { 74 | 75 | setCanMoveSprite(scene.scene.key !== 'MainMenu'); 76 | 77 | }; 78 | 79 | return ( 80 |
81 | phaserRef = el} currentActiveScene={currentScene} /> 82 |
83 |
84 | 85 |
86 |
87 | 88 |
89 |
Sprite Position: 90 |
{`{\n  x: ${spritePosition().x}\n  y: ${spritePosition().y}\n}`}
91 |
92 | 93 |
94 |
95 | ); 96 | }; 97 | 98 | export default App; 99 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Phaser Solid TypeScript Template 2 | 3 | This is a Phaser 3 project template that uses the Solidjs framework and Vite for bundling. It includes a bridge for Solid to Phaser game communication, hot-reloading for quick development workflow and scripts to generate production-ready builds. 4 | 5 | ### Versions 6 | 7 | This template has been updated for: 8 | 9 | - [Phaser 3.88.2](https://github.com/phaserjs/phaser) 10 | - [Solid 1.9.5](https://github.com/solidjs/solid) 11 | - [Vite 6.3.1](https://github.com/vitejs/vite) 12 | - [TypeScript 5.7.2](https://github.com/microsoft/TypeScript) 13 | 14 | ![screenshot](screenshot.png) 15 | 16 | ## Requirements 17 | 18 | [Node.js](https://nodejs.org) is required to install dependencies and run scripts via `npm`. 19 | 20 | ## Available Commands 21 | 22 | | Command | Description | 23 | |---------|-------------| 24 | | `npm install` | Install project dependencies | 25 | | `npm run dev` | Launch a development web server | 26 | | `npm run build` | Create a production build in the `dist` folder | 27 | | `npm run dev-nolog` | Launch a development web server without sending anonymous data (see "About log.js" below) | 28 | | `npm run build-nolog` | Create a production build in the `dist` folder without sending anonymous data (see "About log.js" below) | 29 | 30 | ## Writing Code 31 | 32 | After cloning the repo, run `npm install` from your project directory. Then, you can start the local development server by running `npm run dev`. 33 | 34 | The local development server runs on `http://localhost:8080` by default. Please see the Vite documentation if you wish to change this, or add SSL support. 35 | 36 | Once the server is running you can edit any of the files in the `src` folder. Vite will automatically recompile your code and then reload the browser. 37 | 38 | ## Template Project Structure 39 | 40 | We have provided a default project structure to get you started. This is as follows: 41 | 42 | | Path | Description | 43 | |-------------------------------|-----------------------------------------------------------------------------| 44 | | `index.html` | A basic HTML page to contain the game. | 45 | | `src` | Contains the Solid client source code. | 46 | | `src/index.tsx` | The main **Solid** entry point. This bootstraps the Solid application. | 47 | | `src/vite-env.d.ts` | Global TypeScript declarations, providing type information. | 48 | | `src/App.tsx` | The main Solid component. | 49 | | `src/PhaserGame.tsx` | The Solid component that initializes the Phaser Game and serves as a bridge between Solid and Phaser. | 50 | | `src/game/EventBus.ts` | A simple event bus to communicate between Solid and Phaser. | 51 | | `src/game` | Contains the game source code. | 52 | | `src/game/main.tsx` | The main **game** entry point. This contains the game configuration and starts the game. | 53 | | `src/game/scenes/` | The folder containing the Phaser Scenes. | 54 | | `public/style.css` | Some simple CSS rules to help with page layout. | 55 | | `public/assets` | Contains the static assets used by the game. | 56 | 57 | ## Solid Bridge 58 | 59 | The `PhaserGame.tsx` component is the bridge between Solid and Phaser. It initializes the Phaser game and passes events between the two. 60 | 61 | To communicate between Solid and Phaser, you can use the **EventBus.js** file. This is a simple event bus that allows you to emit and listen for events from both Solid and Phaser. 62 | 63 | ```js 64 | // In Solid 65 | import { EventBus } from './EventBus'; 66 | 67 | // Emit an event 68 | EventBus.emit('event-name', data); 69 | 70 | // In Phaser 71 | // Listen for an event 72 | EventBus.on('event-name', (data) => { 73 | // Do something with the data 74 | }); 75 | ``` 76 | 77 | In addition to this, the `PhaserGame` component exposes the Phaser game instance along with the most recently active Phaser Scene using a Solid ref. 78 | 79 | Once exposed, you can access them like any regular Solid reference. 80 | 81 | ## Phaser Scene Handling 82 | 83 | In Phaser, the Scene is the lifeblood of your game. It is where you sprites, game logic and all of the Phaser systems live. You can also have multiple scenes running at the same time. This template provides a way to obtain the current active scene from Solid. 84 | 85 | You can get the current Phaser Scene from the component event `"current-active-scene"`. In order to do this, you need to emit the event `"current-scene-ready"` from the Phaser Scene class. This event should be emitted when the scene is ready to be used. You can see this done in all of the Scenes in our template. 86 | 87 | **Important**: When you add a new Scene to your game, make sure you expose to Solid by emitting the `"current-scene-ready"` event via the `EventBus`, like this: 88 | 89 | 90 | ```ts 91 | class MyScene extends Phaser.Scene 92 | { 93 | constructor () 94 | { 95 | super('MyScene'); 96 | } 97 | 98 | create () 99 | { 100 | // Your Game Objects and logic here 101 | 102 | // At the end of create method: 103 | EventBus.emit('current-scene-ready', this); 104 | } 105 | } 106 | ``` 107 | 108 | You don't have to emit this event if you don't need to access the specific scene from Soilid. Also, you don't have to emit it at the end of `create`, you can emit it at any point. For example, should your Scene be waiting for a network request or API call to complete, it could emit the event once that data is ready. 109 | 110 | ### Solid Component Example 111 | 112 | Here's an example of how to access Phaser data for use in a Solid Component: 113 | 114 | ```ts 115 | import { IRefPhaserGame } from "./game/PhaserGame"; 116 | 117 | // In a parent component 118 | const ReactComponent = () => { 119 | 120 | let phaserRef; // This will hold the PhaserGame component reference {game, scene} 121 | 122 | const onCurrentActiveScene = (scene: Phaser.Scene) => { 123 | 124 | // This is invoked 125 | 126 | } 127 | 128 | return ( 129 | ... 130 | 131 | ... 132 | ); 133 | 134 | } 135 | ``` 136 | 137 | In the code above, you can get a reference to the current Phaser Game instance and the current Scene by creating a reference with some variable `let phaserRef` and assign to PhaserGame component. 138 | 139 | From this state reference, the game instance is available via `phaserRef.game` and the most recently active Scene via `phaserRef.scene`. 140 | 141 | The `onCurrentActiveScene` callback will also be invoked whenever the the Phaser Scene changes, as long as you emit the event via the EventBus, as outlined above. 142 | 143 | ## Handling Assets 144 | 145 | Vite supports loading assets via JavaScript module `import` statements. 146 | 147 | This template provides support for both embedding assets and also loading them from a static folder. To embed an asset, you can import it at the top of the JavaScript file you are using it in: 148 | 149 | ```js 150 | import logoImg from './assets/logo.png' 151 | ``` 152 | 153 | To load static files such as audio files, videos, etc place them into the `public/assets` folder. Then you can use this path in the Loader calls within Phaser: 154 | 155 | ```js 156 | preload () 157 | { 158 | // This is an example of an imported bundled image. 159 | // Remember to import it at the top of this file 160 | this.load.image('logo', logoImg); 161 | 162 | // This is an example of loading a static image 163 | // from the public/assets folder: 164 | this.load.image('background', 'assets/bg.png'); 165 | } 166 | ``` 167 | 168 | When you issue the `npm run build` command, all static assets are automatically copied to the `dist/assets` folder. 169 | 170 | ## Deploying to Production 171 | 172 | After you run the `npm run build` command, your code will be built into a single bundle and saved to the `dist` folder, along with any other assets your project imported, or stored in the public assets folder. 173 | 174 | In order to deploy your game, you will need to upload *all* of the contents of the `dist` folder to a public facing web server. 175 | 176 | ## Customizing the Template 177 | 178 | ### Vite 179 | 180 | If you want to customize your build, such as adding plugin (i.e. for loading CSS or fonts), you can modify the `vite/config.*.mjs` file for cross-project changes, or you can modify and/or create new configuration files and target them in specific npm tasks inside of `package.json`. Please see the [Vite documentation](https://vitejs.dev/) for more information. 181 | 182 | ## About log.js 183 | 184 | If you inspect our node scripts you will see there is a file called `log.js`. This file makes a single silent API call to a domain called `gryzor.co`. This domain is owned by Phaser Studio Inc. The domain name is a homage to one of our favorite retro games. 185 | 186 | We send the following 3 pieces of data to this API: The name of the template being used (vue, react, etc). If the build was 'dev' or 'prod' and finally the version of Phaser being used. 187 | 188 | At no point is any personal data collected or sent. We don't know about your project files, device, browser or anything else. Feel free to inspect the `log.js` file to confirm this. 189 | 190 | Why do we do this? Because being open source means we have no visible metrics about which of our templates are being used. We work hard to maintain a large and diverse set of templates for Phaser developers and this is our small anonymous way to determine if that work is actually paying off, or not. In short, it helps us ensure we're building the tools for you. 191 | 192 | However, if you don't want to send any data, you can use these commands instead: 193 | 194 | Dev: 195 | 196 | ```bash 197 | npm run dev-nolog 198 | ``` 199 | 200 | Build: 201 | 202 | ```bash 203 | npm run build-nolog 204 | ``` 205 | 206 | Or, to disable the log entirely, simply delete the file `log.js` and remove the call to it in the `scripts` section of `package.json`: 207 | 208 | Before: 209 | 210 | ```json 211 | "scripts": { 212 | "dev": "node log.js dev & dev-template-script", 213 | "build": "node log.js build & build-template-script" 214 | }, 215 | ``` 216 | 217 | After: 218 | 219 | ```json 220 | "scripts": { 221 | "dev": "dev-template-script", 222 | "build": "build-template-script" 223 | }, 224 | ``` 225 | 226 | Either of these will stop `log.js` from running. If you do decide to do this, please could you at least join our Discord and tell us which template you're using! Or send us a quick email. Either will be super-helpful, thank you. 227 | 228 | ## Join the Phaser Community! 229 | 230 | We love to see what developers like you create with Phaser! It really motivates us to keep improving. So please join our community and show-off your work 😄 231 | 232 | **Visit:** The [Phaser website](https://phaser.io) and follow on [Phaser Twitter](https://twitter.com/phaser_)
233 | **Play:** Some of the amazing games [#madewithphaser](https://twitter.com/search?q=%23madewithphaser&src=typed_query&f=live)
234 | **Learn:** [API Docs](https://newdocs.phaser.io), [Support Forum](https://phaser.discourse.group/) and [StackOverflow](https://stackoverflow.com/questions/tagged/phaser-framework)
235 | **Discord:** Join us on [Discord](https://discord.gg/phaser)
236 | **Code:** 2000+ [Examples](https://labs.phaser.io)
237 | **Read:** The [Phaser World](https://phaser.io/community/newsletter) Newsletter
238 | 239 | Created by [Phaser Studio](mailto:support@phaser.io). Powered by coffee, anime, pixels and love. 240 | 241 | The Phaser logo and characters are © 2011 - 2025 Phaser Studio Inc. 242 | 243 | All rights reserved. 244 | --------------------------------------------------------------------------------