# Lesson 17 Complex Sprite Movement

I have a student that wants a sprite to walk left and right but he wants to set the velocity so that when he hits the up arrow the sprite simulates a gravity jump. I don’t know how to do it. Can someone help?

Here is his code:

They are off to a good start. This is a tough problem. The student is going to have to have something that says, if the velocityY < 0(on the way up) then make it a bit less negative and then if velocityY > 0 (on the way down) make it smaller. I think the trick is to get the velocityY get back to exactly zero.

Maybe some others will chime in too.

Hi Nicole,
Another way to approach this would be to make adjustments to the sprite’s `velocityY` based on its current `y` position. You want the sprite to accelerate downwards if it’s “in the air”, and you want it to stop if it’s “on the ground”. Since the sprite’s initial position is 100,300 we can check it like this:

`if (swat.y < 300) { //Is the sprite above the ground? swat.velocityY = swat.velocityY + 0.5; //Accelerate downwards } else { swat.velocityY = 0; //Stop the sprite }`

Looking at the student’s current code, he’ll also want to make sure this code happens before the code that checks for pressing the ‘up arrow’ key. If not, the sprite won’t move it all.
I would suggest cutting lines 26-28, then putting in code similar to what I’ve suggested somewhere before the code in lines 23-25.

By the way, this code might result in the sprite coming to rest when `y` is slightly more than 300, but it shouldn’t be noticeable unless you use a watcher.

1 Like

Here’s a link, if needed: https://studio.code.org/projects/gamelab/JgdzB_H2poHYU9ekKktY8g

Thanks. you are wonderful.