Foundation Game Design with ActionScript 3.0, Second Edition (9 page)

BOOK: Foundation Game Design with ActionScript 3.0, Second Edition
10.46Mb size Format: txt, pdf, ePub

You'll be using the Debug option almost exclusively when you make your games because the information it gives you in the Console and Problems window is extremely useful. Let's find out how by debugging your program and seeing what happens!

From now on, I'm going to refer to the debug option as
compiling.
Whenever I ask you to compile your program in this book, hit that little beetle button.

1. Select Run
Debug from the main menu or click the little beetle icon in the toolbar, as shown in
Figure 1-30
. Flash Builder may ask you if you want to save your work before continuing. If you've run the debugger for this program before, it may ask you if you wish to terminate any existing debugging session. In both cases, click the OK button. It may also ask if you want to open Flash Builder's
debugging perspective
.
If it does, select No. (You'll find out what the debugging perspective is and how to switch to it if you want to near the end of this chapter.)

Figure 1-30.
Choose one of these two possible ways to compile your program.

If Flash Builder is installed properly and you've followed all the instructions in this chapter to the letter, here's what will happen:

  • Flash Builder will launch a Flash Player window. It will be blank. That's just fine; it should be! (If you didn't deselect the “Generate HTML wrapper file” option in the Project Properties panel, a blank web page will open instead. Refer to the section “Tell Flash Builder to Run Your Programs in the Flash Player” near the beginning of this chapter to prevent this from happening.)
  • Flash Builder will also open the Console window. You should see the words, “Hello World! Can you read this?” in the Console window.
    Figure 1-31
    illustrates what you should see. The Console window will also display some technical information that confirms that it has created the SWF file, which is your finished work.

Figure 1-31.
Compile your program and see the result in the Console window.

If this is what happened when you compiled the program, congratulations, you've programmed and run your first computer program! If this didn't happen and you got some sort of strange error messages that you didn't understand, continue reading to see if you can figure out where the problem might lie.

What happened when you compiled the program

Let's assume for a moment that your program worked perfectly, you saw the result I described in the previous section, and you didn't get any error messages. Here's what happened:

When you clicked the Debug button, Flash Builder sent your program to something called the
compiler
. The compiler is a piece of software that is part of the Flex SDK. Its job is to take AS3.0 code and turn it into an SWF file that you can view in the Flash Player. It also checks your code to see if there are any errors. If there are, it warns you, or prevents the code from running if they're serious.

But if the code ran fine, then the compiler displayed your trace message in the Console window and created an SWF file for you in the bin-debug folder of the HelloWorld project directory. Let's see if it actually did this. Open your bin-debug folder and take a look. Your HelloWorld project directory should look something like
Figure 1-32
.

Figure 1-32.
When you compile the program, the AS3.0 compiler creates an SWF file for you in the project folder.

You're interested in the HelloWorld.swf file. SWF files with the same name as your project are what will contain your finished games, which you'll make in later chapters.

But there are also a few more files and folders in that directory. They're not necessary for any of the projects in this book. They're all part of what's known as the
HTML wrapper
. These are the files that you need if you want to upload any of your projects to the Internet. These files will still be created even if you've told Flash Builder not to generate an HTML wrapper, but it won't use them to launch your program in a web browser Let's take a look at what they do—they might be useful to you at some point.

  • HelloWorld.html
    : This is an HTML file (a web page) that the compiler generated for you automatically when you ran and debugged the program. It contains code that plays the SWF file. You can use this HTML file, along with the other files in this directory, as the basis for creating a web page you can publish to the Internet so that others can play your game.
  • swfObject.js
    : This is a JavaScript (JS) file that embeds the SWF file into the HTML page and checks whether the user viewing your game over the Internet has the Flash Player installed.
  • playerProdcutinstall.swf
    : This is a Flash application that prompts users to download the latest version of the Flash Player if the current version isn't compatible with your game.
  • History
    : This folder contains specialized code that lets users navigate your game or web application using the browser's forward and back buttons.

It's beyond the scope of this book to go into the details of publishing HTML files for the web, but all you would need to do in theory is upload these five files and the history folder to a web address (URL) and anyone in the world will be able to play your game. You can find out more about the Flash Builder's HTML wrapper at Adobe's web site at

http://help.adobe.com/en_US/flashbuilder/using/index.html

So that's what you see if everything worked as it should have. But what if….

It didn't work?

There might be an unlucky few of you who did not see the output shown in
Figure 1-31
. In fact, if this is the first time you've ever done any computer programming, it's almost certain that you made a small mistake somewhere that would have prevented the program from running properly. Don't feel bad! There are many things that could have gone wrong and being able to figure out what they are is an essential part of the learning process.

If this is the first time that you've compiled a program with Flash Builder, there's a good chance that Flash Builder wasn't installed properly or you don't have the debug version of the Flash Player installed. If you see an error message saying “Installed Flash Player is not a debugger” then this is the likely cause. Follow the instructions in the section “Using Flash Builder” near the beginning of this chapter to solve this problem.

If Flash Builder was installed properly, your problems will be easy to solve with a bit of careful checking of the code you've written. First, when you hit the Debug button, you might have seen a dialog box called Errors in Workspace, as shown in
Figure 1-33
.

Figure 1-33.
Flash Builder will tell you if you have any problems with your code when you compile the program.

If you see this dialog box, click Cancel. This is important because if you click Proceed, Flash Builder will still try to create the SWF anyway, even though there are errors in your code. This could lead you to the false belief that your program is working properly, when it actually isn't. In a complex game project, with hundreds of lines of code, this could blind you to the fact that there are errors in your code that will manifest as complex bugs that could be difficult to solve or detect.

After cancelling the launch of the SWF, look for the Problems widow at the bottom of Flash Builder's workspace. The Problems window tells you how many errors it thinks you have in your code. Click the small triangle next to where it says Errors, and you'll see a list of exactly what Flash Builder thinks the problems are.
Figure 1-34
shows an example.

Figure 1-34.
Click the triangle next to Errors in the Problems window to see the possible errors in your program.

I can guarantee that whatever it tells you will be completely meaningless to you! It's will take you a lot of time working with Flash Builder to gradually make sense of what these error messages are trying to tell you. However, the term “syntax error” is important. It means that you wrote some code that the AS3.0 language doesn't understand. It's AS3.0's way of saying, “Huh?” This usually indicates that you mistyped or misspelled something. It could also mean that you forgot to open or close one of the curly braces or a parenthesis. Or you just accidentally left something out somewhere. The other confusing thing about these messages is that Flash Builder doesn't really know what's wrong. The error messages are vague. They often don't point to what the real problem is so you'll have to use your powers of deduction to figure out what could be wrong.

Other books

A Groom With a View by Jill Churchill
Dust and Desire by Conrad Williams
Out of Grief by EA Kafkalas
Clash by Nicole Williams
Made For Us by Samantha Chase
Full-Blood Half-Breed by Cleve Lamison
The Bone Man by Wolf Haas
Keep It Pithy by Bill O'Reilly