# Trying to create a jumping character

**URL:** https://discourse.processing.org/t/trying-to-create-a-jumping-character/7448
**Category:** Coding Questions
**Created:** [January 12, 2019, 7:43pm UTC](https://discourse.processing.org/t/trying-to-create-a-jumping-character/7448 "2019-01-12T19:43:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jadenfigger](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@jadenfigger](https://discourse.processing.org/u/jadenfigger)
#### Post date: [January 12, 2019, 7:43pm UTC](https://discourse.processing.org/t/trying-to-create-a-jumping-character/7448/1 "2019-01-12T19:43:45Z")

</div>

I’m trying to create a character that jumps when ever i press the up arrow.

```auto
let r = 20;
let x = 200;
let y = 380;
let speed = 8;
let gravity = 0.2;

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  player();

}

function player() {
    ellipse(x, y, r*2);
  if(keyCode === UP_ARROW) {
    speed -= gravity
    y -= speed;
  }
  if(y > height - r) {
		speed = 0;
    y = 380;
  }
}

```

this is my code right now, but i can’t get the character to stop moving after its reached the bottom of the screen. If anyone could help me, that would be very useful, thanks

---

<div class="post-metadata">

### Author: ![HD161693](https://avatars.discourse-cdn.com/v4/letter/h/ac91a4/32.png) [@HD161693](https://discourse.processing.org/u/HD161693)
#### Post date: [January 13, 2019, 1:31am UTC](https://discourse.processing.org/t/trying-to-create-a-jumping-character/7448/2 "2019-01-13T01:31:17Z")

</div>

Nice work, almost there.

at the start of the program, what is the value of speed? and what is it after you land your jump?  
you can explore this by adding `print(speed);` to the draw() function.  
the speed should probably be reset to 8 if the last key pressed was not the up arrow.

Changing this might lead to other unintended consequences (like being able to chain jumps). Because `(keyCode===UP_ARROW)` doesn’t execute when the up key is pressed, it executes many times while the up arrow was the last key pressed. Later, you might want to make it so you can’t jump if you are already jumping, or to place the `(keycode===UP_ARROW)` code into a `keyPressed()` function so it executes only once.

---

<div class="post-metadata">

### Author: ![jadenfigger](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@jadenfigger](https://discourse.processing.org/u/jadenfigger)
#### Post date: [January 13, 2019, 4:49pm UTC](https://discourse.processing.org/t/trying-to-create-a-jumping-character/7448/3 "2019-01-13T16:49:12Z")

</div>

Thank you, this was really helpful.
