Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 10: Animation > Animating Multiple Items

Animating Multiple Items

This example shows one way to animate multiple items at once. In this case, we use an object-oriented approach to the problem (Java is an object-oriented language, after all). In particular, we create a class for the things we want to animate and then create multiple instances of that class. Being a simple example, we use the same class for all the animating items. However, you can use the same technique to animate different kinds of objects. For example, a tank (an instance of one class) might shoot a bullet (an instance of a different class) at a bunker (an instance of a third class).

The trick is to have each object draw itself and then iterate through them, letting each one draw itself in turn. In that way, the calling class needs to know nothing about how to draw the objects and can focus on the timing and user interface. This kind of organization embodies one of the principles of object-oriented programming, encapsulation, and embodies a primary goal of nearly all software systems, separation of concerns (meaning that each different thing a program can do should be handled by a different part of the program). Those principles are part of the advantage of object-oriented programming, because they make designing programs and finding errors much easier. We get to finding errors in the next chapter.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint