In this article, we'll cover how to download and install Phaser 3 to your HTML5 projects.
1. Create a new folder to house your project. Then, inside that folder, create a folder titled "js" and an index.html file. You can also add assets or anything else you might need to your project directory.
3. On the Phaser 3 download page, click the version link for the most appropriate download option. We recommend getting the latest stable release or the framework version the course recommends.
4. On the version information page, scroll down to the Download section. Then, click one of the download options. We highly recommend choosing phaser.min.js if you intend to make your game available on the internet. However, you can also choose phaser.js if you would prefer.
5. Once downloaded, place your new .js file into the js folder in your project.
6. The last step is to include the library as part of your project. To do so, open your index.html file in the code editor of your choosing and add the following code to make your project recognize the Phaser library:
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <title>Phaser Project</title>
- </head>
- <body>
- <script src="js/phaser.min.js"></script>
- </body>
- </html>
Make sure the src in the script tag matches the name of the file you downloaded. For example, if you downloaded the non-minified version, the correct src would be "js/phaser.js".
Creating Your First Phaser File
This section will briefly demonstrate how to create files for your Phaser project and include them in your project. This step is also good if you'd like to verify Phaser is installed correctly.
1. In the js folder from the previous section. create a new .js file called game.js.
2. In your index.html file, alter the code to include this new script:
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <title>Phaser Project</title>
- </head>
- <body>
- <script src="js/phaser.js"></script>
- <script src="js/game.js"></script>
- </body>
- </html>
3. Let's head back to game.js. If this is the initial file of your game, there are three things that need to happen. We need to create a new scene, set the configuration of the game (height, width, etc), and create the new game while passing the configuration to it. The code below does all three, which you can add to your new .js file.
- // create a new scene
- let gameScene = new Phaser.Scene('Game');
- // set the configuration of the game
- let config = {
- type: Phaser.AUTO, // Phaser will use WebGL if available, if not it will use Canvas
- width: 640,
- height: 360,
- scene: gameScene
- };
- // create a new game, pass the configuration
- let game = new Phaser.Game(config);
4. Verify the project works by opening index.html in any modern web browser. Upon opening, you should see a blank, black screen. By opening the Inspector (or using other development tools, you should also be able to see an indication in the console that Phaser is running.