[Update: This was last updated 3 May 2014.]
Before I get too far into this guide, make sure you have the following things ready to go:
- Tiled 0.9.1, a map editor
- NetBeans IDE 8.x (choose HTML5 & PHP bundle)
- These assets: level.png and character.png
Creating the Project in NetBeans:
Open NetBeans and go to File->New Project…
If it isn’t already, select HTML5 and, under Projects, “HTML5 Application.”
Click “Next >”
For this example, I will be naming the project “BasicPlatformer”.
Make sure “No Site Template” is selected and then click “Next >”
Without selecting any of the libraries, click “Finish”
Once NetBeans has created the project, right-click on “Site Root” and select New and then click on Folder.
One more time, right-click on Site Root, choose New and then Folder.
This time, name this new folder “assets” (as it will hold the image assets) and click Finish again.
There should now be two folders, “js” and “assets”, under the Site Root with the “index.html” file.
Adding the Assets:
From where you have downloaded them, either move the file themselves into the “assets” folder within the “public_html” folder of the NetBeans project, or use the next step. (The NetBeans project folder will be found wherever it was created initially. This is usually under the Documents folder for Windows users.)
One of the quickest ways to move files when using NetBeans is to copy them into the program itself. Simply select the files from some folder and copy them using the keyboard shortcut Ctrl-C or from the right-click file menu.
Then, right-click on a folder within the Projects pane where you want them to go and click on Paste. This will copy the files directly into the project, creating them within the structure.
Regardless of the method used, the “level.png” and “character.png” files should now be in the “assets” folder under the Site Root within NetBeans.
Creating the level with Tiled
From either the File menu or the icon menu, select New.
Set the width to be 15 and the height to be 8 tiles. The individual tile size should be 48 by 48 px. The orientation should remain “Orthogonal” and the Layer format should be “Base64 (zlib compressed)”.
Once the empty map has been created, go to the Map from the top-level menu and select “New Tileset…”
Click on “Browse..” and find the “level.png” file. Select the file, click “OK” and then, when back to the “New Tileset” window, click “OK” once more.
The “level” tileset should now be loaded within the Tileset.
From here, click on the first square in the set and draw a square around the outermost edge of the map. This will serve as the boundaries for the level.
Using the second and then third tiles in the set, draw columns one row in on each side.
Now, using the second-to-last and the last tiles in the set, replace the tiles on the top and bottom edges, stopping each time at the previously created columns. The map should now look like a black square with and inner white rim. This will be the level for the platformer.
As a final step in this process, select “Save As…” from the File top-level menu.
From the drop-down “Save as type” menu, pick “Json files (*.json)”.
Name the file “map” and save it in the folder with the other assets.
Adding the Phaser library to the Project:
If you haven’t already downloaded, we will do that now.
This will download the latest version of all of the code.
Extract the contents of the ZIP file and then enter the “build” folder.
Select the “phaser.min.js” file and, using either the Ctrl-C keyboard shortcut or from the right-click context menu, copy the file.
Within NetBeans, right-click on the “js” folder within the Projects view. Select “Paste” to move a copy of the library into the project.
Writing the Code:
Now that we have added the assets and created a level, we can actually write the code finally.
Highlight all of the HTML within “index.html” and press Delete on the keyboard.
Copy and paste the following code:
(The project is now complete. Click on the Run Project icon or press F6 to see it in action.)
Explaining the Code:
This section is a breakdown of the code by concepts.
Because Phaser is running in a browser environment, it needs to be loaded as part of the HEAD section of a HTML page. I am also using a META tag to tell other devices, usually mobile ones, to set the viewport to the full ‘device-width’ here too.
The BODY itself is somewhat straightforward too. There is a DIV called “phaser-example” that will be the parent element of the game and a SCRIPT tag as well.
The first step to creating a game in Phaser is to do exactly that: create a Phaser.Game object. For its instantiation, we pass its dimensions, which graphics rendering to use, the name of the parent element, and then three functions: the preload, create, and update functions.
Preload is where all loading will take place. Whatever needs to be grabbed and processed by Phaser before the game starts goes here.
Create is where all the creation takes place. Whatever variables need to be created or values set go here.
Update is a function that defines the update-loop for the game. Those values that need to change as a result of pressing buttons or collisions between sprites happens here.
- Within Tiled, use the Map-> “Resize Map…” option to change the length of the map. Make it much longer and use the tiles to paint the level in different patterns, remembering to ‘close’ the level at all points so the character does not fall out of the world. (Remember to save the file and then reload the web page to test the results too.)
- Change the dimensions of the game world. Instead of 720 by 384, make the length shorter or level smaller. Test various numbers and combine this with the above suggestion to create different perspectives of levels.
- Change the amount of gravity to a larger or smaller number. (It is set using player.body.gravity.y.)
- Make the ‘player’ sprite bounce more by increasing the corresponding player.body.bounce.y number.
- Add to or change the look of the tiles in “level.png”. Because Tiled checks the file itself while it is running, it will auto-update the look.
- Consider having some tiles that the character can pass-through by setting a new collision range for the tiles after changing or adding to the original tileset. (This is an easy way to have some tiles be part of the background.)
- Add some threats by introducing new assets, calling game.add.sprite on them, and enabling their physics bodies. Then, test if ‘player’ collides with them during the Update function.