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!
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