1

Here is the boolean expression that I tried to enter:

boolean finish = false;
if (GWINDOW_WIDTH == connectMe.getX())  {
    finish = true;
}
System.out.println(finish);  

GWINDOW_Width is the width of the window

connectMe.getX() is the x coordinate of the dot

I tried to make it so that if the dot touches the edge of the window, the dot finishes the race. However, when running the program, the console window gives the output of "false," even though the dot touched the finish line or the edge of the screen.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
V4k4
  • 33
  • 6

1 Answers1

0

You should change the conditional statement to the following for two reasons:

if (GWINDOW_WIDTH <= connectMe.getX()) {

}
  1. If the dot moves slightly passed the exact edge of the screen you current condition will not be trigger.

  2. If connectMe.getX() is a float or double, you should test for a range because of the way they are stored. See here for more on that topic.

Ryan
  • 1,972
  • 2
  • 23
  • 36
  • Okay thanks. I found that the dot was touching the edge of the screen at the x-coordinate of 940. So here is the code I am using for it: 'boolean finish = (GWINDOW_WIDTH-60) <= connectMe.getX(); if (finish) { } System.out.println("Did the dot finish? : " + finish); ' IT WORKED – V4k4 Nov 14 '17 at 16:10