# 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:** 9\
**Page:** 7

<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:58pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/121 "2019-05-29T20:58:32Z")

</div>

you got the key **5** where say hold2 = true for the right paddle

then here the attachment happens: attachedToPaddle2=true; here:

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

```

wrong:  
if ( xpos \>= 30+size/2) {

you must check it against the right paddle :

if ( xpos \>= width-30-size/2) {

---

<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, 9:08pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/122 "2019-05-29T21:08:32Z")

</div>

ok so now to turn it on i need to type when the 5 key is pressed the hold becomes 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, 9:10pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/123 "2019-05-29T21:10:50Z")

</div>

you have this already!!!

```auto
else if (key == '5'){
     hold2 = !hold2; 
    }

```

---

<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, 9:11pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/124 "2019-05-29T21:11:31Z")

</div>

but when i press 5 it still doesnt hold, maybe i misplaced the code

---

<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, 9:13pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/125 "2019-05-29T21:13:17Z")

</div>

did you correct this

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

```

please find the error yourself, understand your program…

---

<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, 9:19pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/126 "2019-05-29T21:19:13Z")

</div>

i changed as you said the if ( xpos \>= 30+size/2) to if ( xpos \>= width-30-size/2), and added a if (hold2 == 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, 9:20pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/127 "2019-05-29T21:20:27Z")

</div>

does it work?

please find the error yourself, understand your program…

---

<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 30, 2019, 12:09am UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/128 "2019-05-30T00:09:19Z")

</div>

how do i play sounds in processing

---

<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 30, 2019, 2:04am UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/129 "2019-05-30T02:04:29Z")

</div>

Look at library sound on the website

I posted the link yesterday

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