1

I have a program meant to simulate traffic and all process (movement, drawing new cars, responding to traffic lights) except tracking movement are working.

I have used getter methods

    float getXposition(){
        return x;
    }
    float getYposition(){
        return y;
    }

to determine the location of my sprite but my problem is with conditions relating to the position. For example, my do while loop

   do {
      topcar.setYvelocity((topcar.getYvelocity()/2));
   }while(topcar.getYposition() < 275);

is always bypassed.

Update method(s)

public void update() { // update position and velocity every n milliSec
      // default - just move at constant velocity
      x += dx; // velocity in x direction
      y += dy; // velocity in y direction
    }
public void actionPerformed(ActionEvent arg0) {
      // called by Timer every 50 milliseconds
      for (SimpleSprite s : SimpleSprite.sprites)
         s.update(); // update positions of all sprites
      repaint();
    }

What I wanted to do was make it so that when the light turns red the car sprite should slow down to half speed until it reaches y coordinate 275 and then stop, but instead my sprite will abruptly stop as soon as the light turns red

1234 5678
  • 19
  • 5
  • Your do-while-loop will run in one step through. It is equivalent to just set velocity to 0. The reduction of speed should occur only once in an update-step. But it is difficult to see because I can't see how your code is connected. – Ralf Renz May 03 '19 at 08:57
  • 1
    I am surprised that this does not produce an infinite loop as `setYvelocity` does probably not change `Yposition`. You need to implement an animation system that updates velocities and positions in every frame. – Nico Schertler May 03 '19 at 15:41
  • Yes you need to change the architecture of your code maybe this will help: [Path generation for non-intersecting disc movement on a plane](https://stackoverflow.com/a/30639417/2521214) so you need some timer that will update the positions, control their directions and speed, and force render... Here another example of this [How to implement a constraint solver for 2-D geometry?](https://stackoverflow.com/a/40827518/2521214) however both examples are in C++ (VCL based) so in JAVA it will most likely look slightly differently but the architecture of code should be the same... – Spektre May 05 '19 at 06:58
  • In case you have no timers available you can emulate them using infinite loop with `Sleep(1);` inside thread that will fire your timer event but beware of multithreading gfx access (not all APIs allows that safely). Another option is to use OnIdle event ... btw the loop in your question looks suspicious like you are doing things very wrongly I would expect different controlling code/approach or you will get lost in if/else statements soon but I might be wrong as I do not see the context of code and that is just my impression from the few chunks of code I see – Spektre May 05 '19 at 06:59

0 Answers0