# I need help with my code, I don't know why it isn't working

**URL:** https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689
**Category:** Beginners
**Created:** [February 9, 2021, 12:21pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689 "2021-02-09T12:21:39Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![thorax1st](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@thorax1st](https://discourse.processing.org/u/thorax1st)
#### Post date: [February 9, 2021, 12:21pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/1 "2021-02-09T12:21:39Z")

</div>

I have tried to make ellipses with random positions on the canvas which disappear when they are clicked on, I tried doing this by putting an if statement in a for loop which should check if the mouse is inside one of the circles but it doesn’t work and I have no idea why. If the answer seems obvious to you please keep in mind that I am a beginner.  
[my attempt at coding](https://editor.p5js.org/thorax1st/present/KPvFLqT2L)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 9, 2021, 12:33pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/2 "2021-02-09T12:33:36Z")

</div>

Hello!

What is the link to the code please?

This is only the running presentation.

Thanks!

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 9, 2021, 12:34pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/3 "2021-02-09T12:34:01Z")

</div>

Hey, and welcome to the forum!

Great to have you here!

---

<div class="post-metadata">

### Author: ![thorax1st](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@thorax1st](https://discourse.processing.org/u/thorax1st)
#### Post date: [February 9, 2021, 1:26pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/4 "2021-02-09T13:26:57Z")

</div>

excuse me. [is this better?](https://editor.p5js.org/thorax1st/sketches/KPvFLqT2L)

---

<div class="post-metadata">

### Author: ![thorax1st](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@thorax1st](https://discourse.processing.org/u/thorax1st)
#### Post date: [February 9, 2021, 1:27pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/5 "2021-02-09T13:27:56Z")

</div>

thank you Chrisir. I’m happy to be here

---

<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 9, 2021, 1:57pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/6 "2021-02-09T13:57:34Z")

</div>

[http://p5js.SketchPad.cc/sp/pad/view/ro.CaQVvhrEKxAeXd/latest](http://p5js.SketchPad.cc/sp/pad/view/ro.CaQVvhrEKxAeXd/latest)

---

<div class="post-metadata">

### Author: ![thorax1st](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@thorax1st](https://discourse.processing.org/u/thorax1st)
#### Post date: [February 9, 2021, 2:20pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/7 "2021-02-09T14:20:52Z")

</div>

I would much rather know why my code doesn’t work

---

<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 9, 2021, 2:30pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/8 "2021-02-09T14:30:26Z")

</div>

This is your loop inside **mousePressed()** callback:

```javascript
function mousePressed() {
  for (var x = 0; Blasen.length; x++) {
    Blasen.clicked()
    if (Blasen.clicked) {
      Blasen.splice(x,1)
    }
  }
}

```

Compare that w/ mine:

```javascript
function mousePressed() {
  for (let i = balls.length; i--; )
    if (balls[i].isMouseWithinCircle()) {
      const tail = balls.pop();
      if (i !== balls.length) balls[i] = tail;
      redraw();
      break;
    }
  return false;
}

```

The way you attempt to use method **splice()** inside your loop won’t work properly.

But you also lack a proper stop condition in your loop `for (var x = 0; Blasen.length; x++) {`.

---

<div class="post-metadata">

### Author: ![thorax1st](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@thorax1st](https://discourse.processing.org/u/thorax1st)
#### Post date: [February 9, 2021, 2:38pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/9 "2021-02-09T14:38:43Z")

</div>

thanks much apretiated

---

<div class="post-metadata">

### Author: ![thorax1st](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@thorax1st](https://discourse.processing.org/u/thorax1st)
#### Post date: [February 9, 2021, 2:44pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/10 "2021-02-09T14:44:00Z")

</div>

why doesn’t splice work in my for loop. besides it not having an stop condition?

---

<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 9, 2021, 3:04pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/11 "2021-02-09T15:04:45Z")

</div>

B/c when we **splice()** an indexed-based container all elements on the right side where the **splice()** occurs have their indices decreased by 1.

There are 2 workarounds to counter that:

1. Also decrease the current loop iterator variable after each **splice()**.
2. Or do a backwards loop instead, where we start at the last index towards index 0.

In some cases, there’s a 3rd option: Just `break` or `return` after the 1st **splice()**.

That’s what I did to fix your loop (although I find my approach w/ **pop()** for efficient 😉):

```javascript
function mousePressed() {
  for (var i = 0; i < Blasen.length; ++i)
    if (Blasen[i].clicked()) return Blasen.splice(i, 1)
}

```

However your method **clicked()** was in a very bad shape and I had to fix that as well: 🩹

```auto
  clicked() {
    return dist(mouseX, mouseY, this.x, this.y) < this.breite / 2
  }

```

The same **clicked()** method but w/o relying on **dist()**: 🕶

```auto
  clicked() {
    return (mouseX - this.x)**2 + (mouseY - this.y)**2 < (this.breite >> 1)**2
  }

```

For further reading about collision detection go to the link below: 🔗  
[CrHallberg.com/CollisionDetection/Website/point-circle.html](http://CrHallberg.com/CollisionDetection/Website/point-circle.html)

---

<div class="post-metadata">

### Author: ![thorax1st](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@thorax1st](https://discourse.processing.org/u/thorax1st)
#### Post date: [February 9, 2021, 3:15pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/12 "2021-02-09T15:15:52Z")

</div>

thank you very much you have been a great help

---

<div class="post-metadata">

### Author: ![thorax1st](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@thorax1st](https://discourse.processing.org/u/thorax1st)
#### Post date: [February 9, 2021, 3:26pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/13 "2021-02-09T15:26:47Z")

</div>

I have changed it to this but it doesn’t work for some reason[https://editor.p5js.org/thorax1st/sketches/KPvFLqT2L](https://editor.p5js.org/thorax1st/sketches/KPvFLqT2L)

---

<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 9, 2021, 3:32pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/14 "2021-02-09T15:32:51Z")

</div>

Your variable _Blasen_ refers to an array container.

The most common way to access an array’s index is via operator `[]` like I did in my fix:  
`if (Blasen[i].clicked()) return Blasen.splice(i, 1)`

BtW, convention dictates that variables should use **_lowerCaseCamel_**.

And if they refers to a container they also should be a plural or collective name.

---

<div class="post-metadata">

### Author: ![thorax1st](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@thorax1st](https://discourse.processing.org/u/thorax1st)
#### Post date: [February 9, 2021, 3:43pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/15 "2021-02-09T15:43:02Z")

</div>

I have changed the “mousePressed” function to look like your example and also did that with your first example of “clicked”. But it doesn’t work

---

<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 9, 2021, 3:45pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/16 "2021-02-09T15:45:46Z")

</div>

Well, I’m pretty sure if your copy & paste both callback **mousePressed()** & method **clicked()** your sketch should just work. 🤔

---

<div class="post-metadata">

### Author: ![thorax1st](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@thorax1st](https://discourse.processing.org/u/thorax1st)
#### Post date: [February 9, 2021, 3:48pm UTC](https://discourse.processing.org/t/i-need-help-with-my-code-i-dont-know-why-it-isnt-working/27689/17 "2021-02-09T15:48:10Z")

</div>

what ever. I think I’m gonna try something else but thanks for every thing. 😄
