# Train move on mouse button, stop once I click again

**URL:** <https://discourse.processing.org/t/train-move-on-mouse-button-stop-once-i-click-again/10256>\
**Category:** Coding Questions\
**Created:** [April 14, 2019, 10:01am UTC](https://discourse.processing.org/t/train-move-on-mouse-button-stop-once-i-click-again/10256 "2019-04-14T10:01:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![abigail](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@abigail](https://discourse.processing.org/u/abigail)\
**Post date:** [April 14, 2019, 10:01am UTC](https://discourse.processing.org/t/train-move-on-mouse-button-stop-once-i-click-again/10256/1 "2019-04-14T10:01:34Z")

</div>

Hi, I’m making a simple program to make a train move across the screen. At the moment the train moves only when I hold down my mouse button. Would anyone be able to help me make the train move when I press the mouse button once, and then stop once I click it again? 🙂

Thanks!

```auto
Train t;

Chimney c;

void setup() {
  size(600, 400);
  t = new Train();
  c = new Chimney();
}

void draw() {
  background(0, 0, 200);
  t.display();
  t.reset();
  c.display();
  c.reset();

  if (mousePressed) {
    t.move();
    c.move();
  }
}

class Chimney {

  float xChimney, yChimney;
  float xSpeed;

  Chimney() {
    xChimney = 380;
    yChimney = 100;
    xSpeed = 3;
  }

  void display() {
    fill(0, 200, 0);
    rect(xChimney, yChimney, 20, 50);
  }

  void move() {
    xChimney = xChimney + xSpeed;
  }

  void reset() {
    if (xChimney > width) {
      xChimney = -200;
    }
  }
}

class Train {

  float x, y;
  float xSpeed;

  Train() {
    x = 200;
    y = 150;
    xSpeed = 3;
  }

  void display() {
    noStroke();
    fill(200, 0, 0);
    rect(x, y, 200, 100);
  }

  void move() {
    x = x + xSpeed;
  }

  void reset() {
    if (x > width) {
      x = -200;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [April 14, 2019, 10:21am UTC](https://discourse.processing.org/t/train-move-on-mouse-button-stop-once-i-click-again/10256/2 "2019-04-14T10:21:51Z")

</div>

Hi,

Check out the `mouseClicked` function:  
[https://processing.org/reference/mouseClicked\_.html](https://processing.org/reference/mouseClicked_.html)

Then you can have a state variable (let’s call it `move`) that is a boolean.  
Every time you enter the mouseClicked function, you turn on or off the `move` variable.  
In draw you then simply need to check the value of the `move` variable. If it is true you move your train otherwise you do nothing.

---

<div class="post-metadata">

**Author:** ![abigail](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@abigail](https://discourse.processing.org/u/abigail)\
**Post date:** [April 14, 2019, 10:32am UTC](https://discourse.processing.org/t/train-move-on-mouse-button-stop-once-i-click-again/10256/3 "2019-04-14T10:32:57Z")

</div>

Hi, thanks for the reply. I’m still kind of stuck, this is what I have so far…

```auto
Train t;

Chimney c;

void setup() {
  size(600, 400);
  t = new Train();
  c = new Chimney();
}

void draw() {
  background(0, 0, 200);
  t.display();
  t.reset();
  c.display();
  c.reset();
}

void mouseClicked() {
  if (move == true){
  t.move();
  c.move();
  }
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [April 14, 2019, 10:37am UTC](https://discourse.processing.org/t/train-move-on-mouse-button-stop-once-i-click-again/10256/4 "2019-04-14T10:37:01Z")

</div>

Consider this simple sketch and try to understand how it works.

The principle will be the same in your case:

```auto
boolean lightOn;

void setup() {
  size(500, 500);
  lightOn = true;
}

void draw() {
  if (lightOn) {
    background(230);
  } else {
    background(20);
  }
}

void mouseClicked() {
  lightOn = !lightOn;
}

```

---

<div class="post-metadata">

**Author:** ![abigail](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@abigail](https://discourse.processing.org/u/abigail)\
**Post date:** [April 14, 2019, 10:44am UTC](https://discourse.processing.org/t/train-move-on-mouse-button-stop-once-i-click-again/10256/5 "2019-04-14T10:44:41Z")

</div>

Thanks very much! I understand it now (I think haha) 🙂

This is my finished code.

```auto
Train t;
Chimney c;

boolean move = false;

void setup() {
 size(600, 400);
 t = new Train();
 c = new Chimney();
}

void draw() {
 background(0, 0, 200);
 t.display();
 t.reset();
 c.display();
 c.reset();
 
 if (move) {
   t.move();
   c.move();
 }
 
}

void mouseClicked() {
 move = !move;
}

```
