O'Reilly logo

Java™ After Hours: 10 Projects You'll Never Do at Work by Steven Holzner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting the Pucks to Bounce Off Each Other

In the Aquarium application, one fish could swim behind another, but that doesn't work for multiple pucks on the same surface; instead, they're going to bounce off each other. Now that the pucks have all been moved, the run method works through them all to see if any pucks have hit each other. If so, it bounces them (and after the pucks have bounced, the run method checks again to see if they've hit a goal). Here's how the code checks whether one puck has struck another, taking special care not to move a blocker (that is, a Puck object whose immovable method returns true) if it was hit:

 int struckPuck = -1; for (int loopIndex3 = 0; loopIndex3 < 13; loopIndex3++){ Puck testPuck = (Puck)pucks.elementAt ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required