Im working on a small litte program. the player is a ball and can go around in the frame. I want to fix so that another object an apple (i have an apple.png) spawns random everytime you start the game but i dont know which method i should use and how. Im going to make it so everytime the player contacts the apple it dissapears and spawns on another random place with collision detection. im fairly new to java programming.
Asked
Active
Viewed 49 times
-3
-
2http://stackoverflow.com/questions/363681/generating-random-numbers-in-a-range-with-java – Matt_Bro Apr 11 '14 at 12:40
1 Answers
2
Check out the API for Random: http://docs.oracle.com/javase/6/docs/api/java/util/Random.html. Once you have an instance of Random, you can call nextInt(maxValue)
, and it'll return an integer between 0 and that maxValue number. Use that to pick a random X & Y coordinate, and you're off and running.

David
- 2,602
- 1
- 18
- 32