Hey, I'm making a simple moving car at night with its headlight bulbs and I'm using PROCESSING software, not java. What I tried to make is when the car touches the right border, the headlight bulb of the right side of the car should be changed to the left side of the same car. It doesn't work with this code below. Any suggestions?

float moveX, moveY, speed = 3;
float dia;

void setup() {
  size(600, 400);
  moveX = width/2;
  moveY = height/2;
  dia = 50;

void draw() {
  rect(moveX, moveY, dia, 20);
  moveX += speed;
  if (moveX <= 25 || moveX >= width-25)
    speed *= -1;

  fill(255, 255, 0, 150);                               // BELOW THIS POINT, IT DOESN'T WORK!
  if (moveX >= 0 && moveX <= width) {
    triangle(moveX+80, height/2+10, moveX+80, height/2, moveX+20, height/2+5);
    triangle(moveX+80, height/2-10, moveX+80, height/2, moveX+20, height/2-5);
  else if (moveX <= 0 && moveX >= width) {
    triangle(moveX-80, height/2+10, moveX-80, height/2, moveX-20, height/2+5);
    triangle(moveX-80, height/2-10, moveX-80, height/2, moveX-20, height/2-5);
if (moveX <= 0 && moveX >= width) {

This is only going to test true if moveX is exactly width (and width is zero) or width is negative. I'm going to assume that width is never negative since widths usually aren't negative. You shouldn't ever depend on a floating point number to be exactly some value, because floating point arithmetic is not exact. You should be aware that it can happen sometimes and make sure your program behaves appropriately when it does, such as protecting against division by zero, but you should also be aware that it may never happen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.