# How to connect my code with the key pressed event and what should I do to make my character jump? (Duplicate thread)

**URL:** <https://discourse.processing.org/t/how-to-connect-my-code-with-the-key-pressed-event-and-what-should-i-do-to-make-my-character-jump-duplicate-thread/35211>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [February 16, 2022, 8:57pm UTC](https://discourse.processing.org/t/how-to-connect-my-code-with-the-key-pressed-event-and-what-should-i-do-to-make-my-character-jump-duplicate-thread/35211 "2022-02-16T20:57:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RafaDuarte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafaduarte/32/15787_2.png) [@RafaDuarte](https://discourse.processing.org/u/RafaDuarte)\
**Post date:** [February 16, 2022, 8:57pm UTC](https://discourse.processing.org/t/how-to-connect-my-code-with-the-key-pressed-event-and-what-should-i-do-to-make-my-character-jump-duplicate-thread/35211/1 "2022-02-16T20:57:20Z")

</div>

I have been coding a simple game and I’m stuck in the key pressed events and my intention is to make my character move backwards, onwards, upwards and downwards. Where should I place the keypressed code to connect it with the character? And the jumping part of my code should be inside the key pressed code? Please, excuse my lack of knowledge towards Processing once I have been studying it for just a few weeks.

My code:

```auto

// images
PImage background;

// bee character
float charSpeed = 5.5;
float charX, charY;
PImage[] charImage= new PImage[4];
int px, pf;

public void setup() {
  size(800, 600); 
  noFill(); // to adjust the image in the screen properly in fullscreen
  background = loadImage("YellowFlowers.jpg"); 
  background.resize(width, height); 

  // load bee

  charImage[0] = loadImage("bee walk 1.png");
  charImage[1] = loadImage("bee walk 2.png");
  charImage[2] = loadImage("bee walk 3.png");
  charImage[3] = loadImage("bee walk 4.png");
}  

// *******drawing section !*********** //

// background draw
public void draw() {
  image(background, 0, 0); 

  //bee character

  image(charImage[pf], px, 0);
  if (++pf > 3) pf = 0;
}

```

---

<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 17, 2022, 4:55pm UTC](https://discourse.processing.org/t/how-to-connect-my-code-with-the-key-pressed-event-and-what-should-i-do-to-make-my-character-jump-duplicate-thread/35211/2 "2022-02-17T16:55:16Z")

</div>

Place the keyPressed code in draw()

See reference

For jumping you set a negative value addY that you add to y (rising) and increase addY until it’s +2 or so

When player hits the ground say addY=0;

Or something like this

---

<div class="post-metadata">

**Author:** ![RafaDuarte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafaduarte/32/15787_2.png) [@RafaDuarte](https://discourse.processing.org/u/RafaDuarte)\
**Post date:** [February 17, 2022, 5:25pm UTC](https://discourse.processing.org/t/how-to-connect-my-code-with-the-key-pressed-event-and-what-should-i-do-to-make-my-character-jump-duplicate-thread/35211/3 "2022-02-17T17:25:43Z")

</div>

Hi! Thanks for answering my questions. I have already coded the key pressed part and it works nicely, but I still need guidance when it comes to where I should place the jumping code. Should I write it inside Keypressed() alongside with the code for moves?

---

<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 17, 2022, 5:28pm UTC](https://discourse.processing.org/t/how-to-connect-my-code-with-the-key-pressed-event-and-what-should-i-do-to-make-my-character-jump-duplicate-thread/35211/4 "2022-02-17T17:28:51Z")

</div>

> [@RafaDuarte](#):
>
> Should I write it inside Keypressed() alongside with the code for moves?

No, everything inside if(Keypressed) or function Keypressed() does only run as long as the key is hold / or once.

therefore please set the variable addY here.

The usage of addY must take place in the main part and not inside if(Keypressed) or function Keypressed()

---

<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 17, 2022, 5:50pm UTC](https://discourse.processing.org/t/how-to-connect-my-code-with-the-key-pressed-event-and-what-should-i-do-to-make-my-character-jump-duplicate-thread/35211/5 "2022-02-17T17:50:10Z")

</div>

Let’s continue in your other thread
