3 Replies Latest reply: Jun 11, 2010 12:01 PM by 843789 RSS

    Random Moving Algorithm

    843789
      Hello, I'm new to this Java Forum (and also in Java). I am working on a game applet and I need a random moving algorithm, that's what I've coded:
      Random RGen = new Random();
      PlayerX += RGen.nextInt(30);
      PlayerY += RGen.nextInt(30);
      It does nothing. I placed it in run(), and I also tried it as a function (as well called in run() ). Can someone help?
        • 1. Re: Random Moving Algorithm
          Kayaman
          UMihnea wrote:
          It does nothing.
          Sure it does. The error is in the code that you didn't show.
          • 2. Re: Random Moving Algorithm
            843789
            UMihnea wrote:
            Hello, I'm new to this Java Forum (and also in Java). I am working on a game applet and I need a random moving algorithm, that's what I've coded:
            Random RGen = new Random();
            PlayerX += RGen.nextInt(30);
            PlayerY += RGen.nextInt(30);
            It does nothing. I placed it in run(), and I also tried it as a function (as well called in run() ). Can someone help?
            It's not immediately related to the problem you report but note that you shouldn't create new Random objects all the time. It should be sufficient to create just one Random object (or a few at the most in different parts of the applet).
            • 3. Re: Random Moving Algorithm
              843789
              Ok, after few tests, I found that my code is working and I put it in a function (+randomMove()+). I'm searching for a method for my object to get back to a specific X, and I wrote this:
              if (PlayerX > WIDTH - 15){ 
                   randomMove(6,5);
              }else if (PlayerX < 15){ 
                   PlayerX = 70;
              }