# Calling classes that won't loop

**URL:** <https://discourse.processing.org/t/calling-classes-that-wont-loop/8623>\
**Category:** Coding Questions\
**Created:** [February 22, 2019, 10:53am UTC](https://discourse.processing.org/t/calling-classes-that-wont-loop/8623 "2019-02-22T10:53:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maximos118](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/maximos118/32/3811_2.png) [@Maximos118](https://discourse.processing.org/u/Maximos118)\
**Post date:** [February 22, 2019, 10:53am UTC](https://discourse.processing.org/t/calling-classes-that-wont-loop/8623/1 "2019-02-22T10:53:07Z")

</div>

Hi there! I’m currently working on a game with p5 which will include some pretty complicated stuff… at least for me anyway. Hopefully you good looking people will help me to finish it in the future!

Anyway my first of many questions would be… how do I call a class without it looping in p5?

Consider my following code:

```auto
function draw() {
  lumberjack.clicked();
}

class Lumberjack {
  constructor(x, y, br, bg, bb, dr, dg, db) {
    this.x = x,
    this.y = y,
    this.br = br,
    this.bg = bg,
    this.bb = bb,
    this.dr = dr,
    this.dg = dg,
    this.db = db
  }
  body() {
    noStroke();
    fill(this.br, this.bg, this.bb);
    rect(this.x, this.y, 20, 40);
    fill(this.dr, this.dg, this.db);
    rect(this.x + 4, this.y - 2, 2, 35);
    rect(this.x + 14, this.y - 2, 2, 35);
    rect(this.x, this.y + 30, 20, 10);
    fill(91, 51, 0);
    rect(this.x, this.y + 30, 20, 2);
    fill(255);
    rect(this.x + 9, this.y + 30, 2, 2);
  }
  move() {

  }
  clicked() {
    let d = dist(mouseX, mouseY, this.x + 10, this.y + 20);
    if (d < 20) {
      console.log("I'm a lumberjack and I'm ok");
    }
  }
}

```

If I click on my lumberjack an infinite loop of console.logs starts.

The only way I could sort of figure out how to solve this is to put my function call in a mousePressed() function but then my console.log is still called twice.

Can I call classes without having them loop?

Thanks in advance!

Max

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 22, 2019, 11:22am UTC](https://discourse.processing.org/t/calling-classes-that-wont-loop/8623/2 "2019-02-22T11:22:06Z")

</div>

[Beautifier.io](http://Beautifier.io)

> **[Toggle Rects](https://bl.ocks.org/GoSubRoutine/78bcd2ae59f6db72d5e6736589d2856b)**
>
> GoSubRoutine’s Block 78bcd2ae59f6db72d5e6736589d2856b

---

<div class="post-metadata">

**Author:** ![Maximos118](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/maximos118/32/3811_2.png) [@Maximos118](https://discourse.processing.org/u/Maximos118)\
**Post date:** [February 22, 2019, 11:26am UTC](https://discourse.processing.org/t/calling-classes-that-wont-loop/8623/3 "2019-02-22T11:26:04Z")

</div>

Can you explain a little bit please? 🙂

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 22, 2019, 11:32am UTC](https://discourse.processing.org/t/calling-classes-that-wont-loop/8623/4 "2019-02-22T11:32:23Z")

</div>

- “Toggle Rects” is a sketch which contains a `class` called Rect.
- Each time the canvas receives a mouse click, it iterates over all instances of Rect.
- AFAIK, 1 click is 1 Rect color toggle.
