Game Development Reference
The program figures out whether the object is being dragged by accessing the object's getter.
eo>aejc@n]cca` is pnqa when the object “is being dragged” and b]hoa when it isn't.
You should also see the great advantage of using object-oriented programming: the work of the pro-
gram is distributed between two classes. Each class is responsible for handling only those tasks that
directly relate to it. Although the overall structure is more complex because you're dealing with two
classes rather than just one, the individual complexity of those classes is much less. In the long run,
this saves you much more time when it comes to changing features and debugging.
Another little trick introduced in this example is a scripted motion technique called easing , which is
the effect that you can see as objects gently “ease” into position over the targets.
Easing is very easy to implement using a simple formula inside an AJPAN[BN=IA event:
You can make the animation happen faster or slower by changing the easing value. In the program,
this value is stored in a variable called [a]oejc and has a value of ,*/. Changing it to a higher num-
ber, such as 0.5, makes the easing effect happen much more quickly. Changing it to a lower number,
such as 0.1, creates a much slower effect. The code that makes the objects ease into position over the
target looks like this:
The code adds the easing formula to the t and u positions of the object to make it move to the new
position. Easing is a basic technique in game design to move objects, and you'll be looking at many
more examples throughout this chapter.
An alternative to inheritance: Composition
Up until now, you've been creating objects using inheritance. You might recall that inheritance is the
system of extending a class to create a new class. All the classes so far have been created by extend-
ing the Ikrea?hel class. When you see the keyword atpaj`o in the class definition, you know that
inheritance is at work:
lq^he__h]ooJas?h]oo atpaj`o Ikrea?hel
This allows the new class to “inherit” all the properties and methods of the class it extends. The new
class can then use all those properties and methods in addition to any new properties and methods
that the new class defines. Inheritance is a quick, easy, and flexible way to make new objects based
on old ones.
There is an alternative way to creating objects using classes called composition . To get more insight
into object-oriented programming, you'll take a brief look at how composition works.
In the example drag-and-drop program you just examined, both the na`Omq]na and ^hqaOmq]na
objects share the same class. Although binding symbols to a common base class is usually the simplest
way for symbols to share a class, you can also do it directly with code using composition.