# Solving an exercise in Dan Shiffman "Learning Processing"

**URL:** <https://discourse.processing.org/t/solving-an-exercise-in-dan-shiffman-learning-processing/8817>\
**Category:** Coding Questions\
**Created:** [February 28, 2019, 8:42pm UTC](https://discourse.processing.org/t/solving-an-exercise-in-dan-shiffman-learning-processing/8817 "2019-02-28T20:42:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [February 28, 2019, 8:42pm UTC](https://discourse.processing.org/t/solving-an-exercise-in-dan-shiffman-learning-processing/8817/1 "2019-02-28T20:42:37Z")

</div>

Trying to solve Exercise 5-8 (p.83) in D. Shiffman book “Learning Processing”

Keep getting an error message: **"unexpected token: void"**

Goal: circle starts moving in sketch once the mouse has been pressed.

**Here is my code:**

```auto
boolean moving = true;

int circleX = 0;
int circleY = 100;

void setup() {
  size(200, 200);
}
void draw() {
  background(100);
  stroke(255);
  fill(0);
  ellipse(circleX, circleY, 50, 50);

  if (moving) {
    circleX = circleX+1;
  }
 void mousePressed() {
    moving = !moving;
  }
}

```

What am i missing?  
Thank you!

---

<div class="post-metadata">

**Author:** ![seeblank](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@seeblank](https://discourse.processing.org/u/seeblank)\
**Post date:** [February 28, 2019, 9:35pm UTC](https://discourse.processing.org/t/solving-an-exercise-in-dan-shiffman-learning-processing/8817/2 "2019-02-28T21:35:52Z")

</div>

Hey there, You got your mousePressed as a void statement, but it is inside the draw() function. Take it out of the draw(), and it should work:

```auto
boolean moving = true;

int circleX = 0;
int circleY = 100;

void setup() {
  size(200, 200);
}
void draw() {
  background(100);
  stroke(255);
  fill(0);
  ellipse(circleX, circleY, 50, 50);

  if (moving) {
    circleX = circleX+1;
  }
}

void mousePressed() {
  moving = !moving;
}

```

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [February 28, 2019, 9:44pm UTC](https://discourse.processing.org/t/solving-an-exercise-in-dan-shiffman-learning-processing/8817/3 "2019-02-28T21:44:38Z")

</div>

Thank you!! That worked. 🙂

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 3, 2019, 12:57am UTC](https://discourse.processing.org/t/solving-an-exercise-in-dan-shiffman-learning-processing/8817/5 "2019-03-03T00:57:02Z")

</div>

Note that there are two things called mousePressed – a function, which is called whenever the mouse is pressed, and a variable, which is true whenever the mouse is pressed.

Use the variable in draw – use the function outside.

- [https://processing.org/reference/mousePressed\_.html](https://processing.org/reference/mousePressed_.html)
- [https://processing.org/reference/mousePressed.html](https://processing.org/reference/mousePressed.html)

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [March 6, 2019, 8:51pm UTC](https://discourse.processing.org/t/solving-an-exercise-in-dan-shiffman-learning-processing/8817/6 "2019-03-06T20:51:55Z")

</div>

Thank you! Being new to coding, this further clarification of the **function vs the variable** usage is very helpful!! Much appreciated.
