Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Commands, or functions, are the basic building blocks of a program. A function lets you define specific steps a computer should take. You then make the computer perform those steps by "calling" the function. Learn about functions while adding to a JavaScript game.
Using Functions
-
Create or "declare" a function. This stores the steps of the function in memory. The function doesn't activate, but the computer remembers what to do:
function add(x, y) { alert( x + y); }
-
"Call" a function. Make the function do something by calling it by name and giving it some information:
This will make an alert box open in your browser with the number 5 in it.add(2, 3);
Downloading and Running the Project Files
Because of how the Phaser.JS library handles files, you need to be running a web server locally to view a working copy of this project. You can't just open the index.html file in your web browser as you might a basic HTML page. There are several simple ways to get a web server up and running on your computer:
- The Web Server for Chrome is an easy-to-install extension for Chrome that lets you select a folder on your computer (like the folder with the game files in them) and deliver them through a basic web server. This is probably the easiest way to get started — but you'll need to use the Chrome web browser.
- If you don't want to use Chrome, check out a simple guide for getting a local web server set up for several other methods.
You need to sign up for Treehouse in order to download course files.
Sign up