# Moving two Objects at the same time with different keys

**URL:** <https://discourse.processing.org/t/moving-two-objects-at-the-same-time-with-different-keys/26097>\
**Category:** Beginners\
**Created:** [December 9, 2020, 8:23pm UTC](https://discourse.processing.org/t/moving-two-objects-at-the-same-time-with-different-keys/26097 "2020-12-09T20:23:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jorick](https://avatars.discourse-cdn.com/v4/letter/j/c89c15/32.png) [@jorick](https://discourse.processing.org/u/jorick)\
**Post date:** [December 9, 2020, 8:23pm UTC](https://discourse.processing.org/t/moving-two-objects-at-the-same-time-with-different-keys/26097/1 "2020-12-09T20:23:36Z")

</div>

I´m working on a game in wich i have two balls. Both Balls should be movable at the same time, the first one with w,a,s,d and the second one with the arrow keys. My problem is that i can always only move one object. Are there some fundamental steps i missed out? please help me!

```auto
              // Ball 1 movement
void draw(){
  if(keyPressed){

    if (key == 'w' ){

      ballY1 = ballY1 - 3;
     
    }

    if (key == 's' ){

    ballY1 =ballY1 + 3;     
       
    }

    if (key == 'a'){

      ballX1 = ballX1 - 3;     
       
    }

    if (key == 'd' ){

      ballX1 = ballX1 + 3;
      
    }

               // Ball 2 movement

    if(keyPressed){

    if (keyCode == UP ){

      ballY2 = ballY2 -3;
      
      
    }
    if (keyCode == DOWN ){

      ballY2 = ballY2 + 3;     
       
    }

   if (keyCode == LEFT){

      ballX2 = ballX2 - 3;      
       
    }

    if (keyCode == RIGHT){

       ballX2 = ballX2 + 3;         
      
    }
}

```

This is not my whole code!!! I left out all the other code like drawing the ellipses or the variables because it might be distracting and i only need help with this specific thing.

---

<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:** [December 9, 2020, 8:41pm UTC](https://discourse.processing.org/t/moving-two-objects-at-the-same-time-with-different-keys/26097/2 "2020-12-09T20:41:09Z")

</div>

> [@jorick](#):
>
> My problem is that i can always only move one object.

https://www.openprocessing.org/sketch/718355/embed/

---

<div class="post-metadata">

**Author:** ![jorick](https://avatars.discourse-cdn.com/v4/letter/j/c89c15/32.png) [@jorick](https://discourse.processing.org/u/jorick)\
**Post date:** [December 9, 2020, 9:04pm UTC](https://discourse.processing.org/t/moving-two-objects-at-the-same-time-with-different-keys/26097/3 "2020-12-09T21:04:30Z")

</div>

Thanks for your fast reply but is there any simpler solution because i dont really understand the code 😌im sorry i´m learning processing in school right now

---

<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:** [December 9, 2020, 9:42pm UTC](https://discourse.processing.org/t/moving-two-objects-at-the-same-time-with-different-keys/26097/4 "2020-12-09T21:42:58Z")

</div>

> [@jorick](#):
>
> … I don’t really understand the code.

You can ask where you don’t get it. 😉

> [@jorick](#):
>
> … but is there any simpler solution…

The main parts of my sketch is the **keyPressed()** & **keyReleased()** callbacks: 🤓

> **[keyPressed() / Reference](https://processing.org/reference/keyPressed_.html)**
>
> The keyPressed() function is called once every time a key is pressed. The key that was pressed is stored in the key variable. For non-ASCII keys, use the keyCode varia…

> **[keyReleased() / Reference](https://processing.org/reference/keyReleased_.html)**
>
> The keyReleased() function is called once every time a key is released. The key that was released will be stored in the key variable. See key and keyCode for more informati…

And some container to store which _keyCode_ values are currently active: ⌨  
`final byte[] keysDown = new byte[KEYS_RANGE];`

> **[keyCode / Reference](https://processing.org/reference/keyCode.html)**
>
> The variable keyCode is used to detect special keys such as the UP, DOWN, LEFT, RIGHT arrow keys and ALT, CONTROL, SHIFT. When checking for these keys, it can be useful to first chec…
