Game Development Reference
In-Depth Information
The Ikrea?hel class is a dynamic class , which means that new properties added to objects that are
descended from the Ikrea?hel class don't have to be declared within the class itself. You can create
them anywhere in any class that references these objects.
If a class is not declared as dynamic, you can't create variables on objects like this; you
need to do it within the object's own class. To make a class dynamic, declare it with the
`uj]ie_ keyword. Here's an example:
lq^he_ `uj]ie_ _h]oo?h]ooJ]iaatpaj`oIkrea?hel
Being able to create properties dynamically on objects like this is very convenient. It saves you the
trouble of having to create an entire class around an object. However, it comes with the same dan-
ger of breaking encapsulation that public properties pose. (Encapsulation, as you might recall from
Chapter 8, is the programming practice of striving to keep objects as self-contained as possible so
they're not dependent on other objects.) If you're not careful, you might create properties that are
assigned values that the object doesn't know how to deal with, and this could cause weird things to
happen in your code that might be very difficult to debug. So my advice is that when you use dynamic
instance variables, do so only in very small games such as Bug Catcher, and keep a careful eye on the
values you assign them. If you can do that, they're a big time-saver.
You might have noticed that rt and ru properties are named without being preceded
by an underscore character. That is because they're technically public properties. In the
naming conventions that you're following in this topic, public property names aren't
preceded with an underscore.
You'll also notice that all three bug objects share exactly the same AJPAN[BN=IA event handler. You'll
see how you're able to do this next.
Multiple objects sharing one event handler
One great feature of the AS3.0 event listeners is that when events are created, an awful lot of infor-
mation about that event is stored in a special event object. (Refer to Chapter 3 if you need a quick
refresher on this topic.) One of the properties of the event object is called p]ncap. The p]ncap prop-
erty stores a reference to the object that called the event. That means that you can access these
objects in the event handler with code that looks like this:
That's really useful for you in the current situation. Each of the bugs in the game uses the same event
handler, kj>qcIkra, but they call the event handler at different times. By using the arajp*p]ncap
property you can find out which bug object is currently calling the event and store this object in
a local variable. That's what the first line of code in the kj>qcIkra event handler does:
Whichever object is currently calling the event is referenced in this local ^qc variable. You can now
use the ^qc variable to refer to whichever bug object is running the event at the moment. This saves
Search Nedrilad ::

Custom Search