0

Every time i restart a game of Pong i want the ball to appear in random position.

add(ball);
startX = rand.nextInt();
startY = rand.nextInt();
ball.setLocation(startX, startY);

This is the code I used but it makes my game freeze for one second every frame and I don't know the reason

Asalas77
  • 249
  • 5
  • 13
  • 3
    `nextInt()` will give you numbers up to 2^31 (and negative ones too), so you should use something like [`nextInt(limitX/Y)`](http://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int-). As this will only give you positive numbers (and you may need a negative coordinate), you can also use `nextInt() % limit`, which will also work with negative numbers. – Steffen Apr 03 '14 at 17:57
  • 2
    Check out http://stackoverflow.com/questions/363681/generating-random-numbers-in-a-range-with-java to see how to generate random numbers within a range. use [0, screensize] as your range. – Arrem Apr 03 '14 at 18:03
  • i did `startX = rand.nextInt(getWidth());` and it works – Asalas77 Apr 03 '14 at 18:04
  • you mention "Every frame", are you talking about the programming "frame", i.e. animation frames, the Java frame, i.e. java.awt.Frame, or some other kind of frame? – Mike 'Pomax' Kamermans Apr 03 '14 at 18:06
  • Yes, it also works because `rand.nextInt(number)` generates a number in [0, number). – Arrem Apr 03 '14 at 18:09
  • @Mike'Pomax'Kamermans my program runs in a loop, each iteration a number of operations are made (move ball by x pixels, get mouseY position, move paddle to this position etc) and it ends with `pause(1000/48)` which should generate roughly 48 fps (this logic might be wrong, but I've been told so by my lecturer). I used "every frame" as in "every iteration", it was just my mental shortcut. Now that I think about it it's not every frame, because `nextInt();` is not in the loop, but it just looked like it: something moves -> 1 second freeze -> move -> freeze etc – Asalas77 Apr 03 '14 at 18:23
  • that logic is most definitely wrong: you will get 48 frames per second only if you don't actually have any code to run each frame. As your code will take at least several milliseconds to run, you're actually going to get a value significantly below 48 fps. But that said: it sounds like you're saying the 1 second freeze is not related to nextInt. We can check this fairly easily by adding a `System.out.println(System.nanoTime())` before and after the nextInt calls. This'll tell you how long it took the JVM to run those two lines. – Mike 'Pomax' Kamermans Apr 03 '14 at 18:29
  • @Mike'Pomax'Kamermans The question has alreaby been answered, but i'm still curious about the freeze. The freezes are somehow caused by `nextInt();` with no limit. `startX = rand.nextInt(getWidth());` works, but `startX = rand.nextInt(/*getWidth()*/);` is freezing, which is weird to me, even when calculating 2^31 this line of code should only be executed once at the beginning, right? – Asalas77 Apr 03 '14 at 18:37
  • @Mike'Pomax'Kamermans i read through my code again and found the reason. It's here: http://pastebin.com/u2stRLLz . When generated int is well over window size, `ballOffScreen();` is called all the time, with method `bounce()` containing `nextInt();` therefore random 2^31 is calculated multiple times within miliseconds – Asalas77 Apr 03 '14 at 18:42
  • if the question was solved without an answer, it's a good idea to unpost it. That said, I tried https://pastebin.mozilla.org/4755027 and see no difference between nextInt() and nextInt(num). – Mike 'Pomax' Kamermans Apr 03 '14 at 18:45

0 Answers0