In this article, we'll cover how to download and install Phaser 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. From the top menu, hit "Download" to head to the downloads page.
4. On the downloads page, hit the "Download Phaser from GitHub" button to continue.
5. Download the appropriate file. We highly recommend choosing min.js if you intend to make your game available on the internet. However, you can use the regular js option as well.
6. Once downloaded, place your .js file into the js folder in your project.
7. 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.js"></script>
- </body>
- </html>
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.
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. 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.
You will also see that there is an indication in the Console window that Phaser is indeed working.