Game Development Reference
Programming with the Collision class
If you understand this basic concept, all you need to do is figure out how to represent this logic in
code. You'll do that in a moment, but you'll be doing it very differently to any of the other code
you've added to the program so far in this topic. You'll actually use this code as part of a completely
separate class. The class is called ?khheoekj, and you'll find it in the chapter's source files.
Up until now, all the programming that you've done has been inside a class called I]ej. That's been
great for learning purposes and the short programs you've been building, but as your games increase
in complexity you'll find that it's vastly more efficient to break components of your games up into
separate classes. How to do this will comprise much of the substance of the rest of the topic, so don't
panic if this seems daunting at the moment! You'll take everything in small, manageable steps.
The reason why you should put all this new axis-based collision code into a new class is because it will
be so useful that you'll almost certainly want to use it in all your games from now on. Instead of having
to write it over and over again each time you design a new game, if it's in its own class you can write
the code once and then use the class in any game that might need it. That's one of the great things
about using classes; you can easily reuse code you've written for other projects.
Using static methods
Inside the new ?khheoekj class is a method called ^hk_g that blocks the lh]uan object's movement.
Unlike any of the methods created in previous chapters, the ^hk_g method is a special kind of method
called a static method .
Static methods are usually designed to perform a general task that can be of use at many different times
and in many different contexts in your program. Maybe you'll find that in your game you don't only want
walls to block the player's movement you also want trees, locked doors, or even enemies. Instead of writ-
ing separate (and almost identical) methods to do this for each object, it's much better to write a single
general method that all these objects can use if they want to. Static methods allow you to do just that.
To use a static method in your program, simply give the name of the class that the method belongs to,
followed by a dot, and then the method name. Here's an example:
You've seen static methods before; you just weren't aware that it's what they were called. In Chapter 5,
you used one of AS3.0's special built-in methods:
Look familiar? Yes, it's a static method! n]j`ki is a method that is part of AS3.0's I]pd class. It does
the specialized job of giving you a random number between 0 and 1, and you can use it anywhere in
the program inside any other class. Static methods used like this are often called utilities . They do
a useful little job for you in your program, and you don't need to worry about how they work as long
as they provide the result you need.
You can use the ^hk_g method inside the ?khheoekj class, anywhere in the program, like this:
You just need to make sure that the ?khheoekj*]o file is in the same folder as the class that's using it.
You'll soon see how easy it is to use.