# Holding the ball in pong

**URL:** <https://discourse.processing.org/t/holding-the-ball-in-pong/11520>\
**Category:** Coding Questions\
**Created:** [May 23, 2019, 8:09pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520 "2019-05-23T20:09:47Z")\
**Posts on this page:** 20\
**Page:** 6

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 6:39pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/101 "2019-05-29T18:39:02Z")

</div>

something i noticed on the last version,of the code you posted and the ball doesnt seem to leave the paddle when the space bar is pressed again

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 7:09pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/102 "2019-05-29T19:09:29Z")

</div>

oh yeah and the ball isnt following the paddle anymore

---

<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:** [May 29, 2019, 7:15pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/103 "2019-05-29T19:15:01Z")

</div>

You‘ll solve this eventually

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 7:16pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/104 "2019-05-29T19:16:37Z")

</div>

ok i fixed the space bar issue, i just had to switch the else if to just if

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 7:20pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/105 "2019-05-29T19:20:39Z")

</div>

ok i fixed both issues

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 7:36pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/106 "2019-05-29T19:36:29Z")

</div>

what did you meant on the line hold = !hold, why not hold + true?

---

<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:** [May 29, 2019, 7:38pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/107 "2019-05-29T19:38:23Z")

</div>

this means:

```auto
hold = 
    ! hold;

```

The ! means not so, it makes hold become its opposite:

when it’s false, it becomes true and vice versa

so it’s toggle

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 7:56pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/108 "2019-05-29T19:56:09Z")

</div>

so for the hold2 if just copy this replacing it with hold2 and attachedToPaddle2?

```auto
  if (! (winplayer1||winplayer2)) {
    if (hold && attachedToPaddle) 
    {
      // ignore
    } else 
    {
      // move ball
      //xspeed *= 1.001;
      xspeed += 0.001;

      xpos += xspeed;
      ypos += yspeed;
    }
  }

```

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 7:57pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/109 "2019-05-29T19:57:04Z")

</div>

same thing with theese?

```auto
if ( xpos <= 30+size/2) {
    if (ypos > playerpos1-50 && ypos < playerpos1+50) {
      if ( ! (hold && attachedToPaddle)) {
        xspeed *= -1;
        yspeed += (ypos-playerpos1)/5;
      }
      attachedToPaddle=true;
    }
  } else {
    attachedToPaddle=false;
  }

```

```auto
} if (hold && attachedToPaddle) {
      ypos = -8;
    }

```

---

<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:** [May 29, 2019, 8:06pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/110 "2019-05-29T20:06:03Z")

</div>

You should try to understand the purpose of these lines and then implement the changes

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 8:14pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/111 "2019-05-29T20:14:45Z")

</div>

so this one stops the ball when a player wins?

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 8:15pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/112 "2019-05-29T20:15:06Z")

</div>

the if (! (winplayer1||winplayer2)) ?

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 8:16pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/113 "2019-05-29T20:16:05Z")

</div>

} if (hold && attachedToPaddle) {  
ypos = -8;  
} this one makes the ball follow the paddle

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 8:16pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/114 "2019-05-29T20:16:36Z")

</div>

and this if ( xpos \<= 30+size/2) {  
if (ypos \> playerpos1-50 && ypos \< playerpos1+50) {  
if ( ! (hold && attachedToPaddle)) {  
xspeed \*= -1;  
yspeed += (ypos-playerpos1)/5;  
}  
attachedToPaddle=true;  
}  
} else {  
attachedToPaddle=false;  
}

holds the ball?

---

<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:** [May 29, 2019, 8:21pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/115 "2019-05-29T20:21:48Z")

</div>

> [@somedude](#):
>
> if (! (winplayer1||winplayer2)) ?

There are several things going on. This line says don’t execute when a player has won

---

<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:** [May 29, 2019, 8:23pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/116 "2019-05-29T20:23:07Z")

</div>

> [@somedude](#):
>
> } if (hold && attachedToPaddle) {  
> ypos = -8;  
> } this one makes the ball follow the paddle

That’s right, ball follows paddle when it’s attached

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 8:23pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/117 "2019-05-29T20:23:56Z")

</div>

so by the logic this one

```auto
if ( xpos <= 30+size/2) {
if (ypos > playerpos1-50 && ypos < playerpos1+50) {
if ( ! (hold && attachedToPaddle)) {
xspeed *= -1;
yspeed += (ypos-playerpos1)/5;
}
attachedToPaddle=true;
}
} else {
attachedToPaddle=false;
}

```

holds the ball

---

<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:** [May 29, 2019, 8:29pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/118 "2019-05-29T20:29:01Z")

</div>

No.

it changes the direction of the ball on the paddle but only the first time when we have an attached situation

Then we set attached to true

Might help to hit ctrl-t and check the indents

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 8:31pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/119 "2019-05-29T20:31:40Z")

</div>

so i copy that one and change it acordingly

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 29, 2019, 8:46pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/120 "2019-05-29T20:46:28Z")

</div>

im trying to write a function for the right paddle to hold, i still dont understand how the attache to paddle fdunction works

[Previous page](https://discourse.processing.org/t/holding-the-ball-in-pong/11520.md?page=5)

[Next page](https://discourse.processing.org/t/holding-the-ball-in-pong/11520.md?page=7)
