• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Falling rocks in an applet?

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is code for a game I am making where you are a character at the bottom of the screen dodging falling rocks. I was wondering how to add in the falling rocks. I think I need to use Timer and an ArrayList but I am not sure if that would work and not truly sure how to go about doing it. Any help or tips would be appreciated. Thanks!

Posts: 3323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I would suggest you create a 'Rock' class that extends Component. The Rock Class needs to override paint() to draw a rock and also needs a method to move it to the next screen location. It may also be required to know how fast it is moving and what direction it is moving in (depending how complex your game is). Also you probably need some hit logic to know if it has touched another item such as another rock or the player.

You are correct in your idea of using a Timer and some form of collection to holds all your rocks. When the game is running you create instances of your Rock class for each rock that is currently falling and each time the timer fires you move the rocks to their next location and call repaint() to redraw the screen. In the paint method you draw the background and the player and then iterate over the collection of rocks and pass the current graphics object to each rock's paint method.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic