How can I make my rectangle "jump" in code?

Monkey Forums/Monkey Programming/How can I make my rectangle "jump" in code?

DucksGonnaDuck(Posted 2015) [#1]
Well I just got Monkey X, like it so far. I made the rectangle game where I use WASD to move it. I want the space bar (KEY_SPACE) to make the rectangle "jump" as in going up by 6 pixels and going down by 6 pixels later.

Right now I have:

If KeyDown (KEY_SPACE) Then y = y - 6

But obviously that just makes it go up, what would I have to type next or before to make it go back down? Thank you!


Nobuyuki(Posted 2015) [#2]
you need to store the amount jumped in a variable. Here's a very simple example.

Field Jumping:Bool
Field JumpAmt:Int

Method OnUpdate()
  If Jumping and JumpAmt < 6  'Move upwards
    JumpAmt +=1
 ElseIf Jumping  'The first test failed.  If we're still jumping, turn that off so the jump amount can go back to normal.
    Jumping = False
  ElseIf Not Jumping and JumpAmt > 0   'Move downwards
    JumpAmt -=1
  End 

  If KeyDown (KEY_SPACE) and JumpAmt = 0 Then Jumping = True
End 


Method OnRender()
    DrawImage (img, x, y - JumpAmt)  'Make sure to subtract JumpAmt from your y value in your own code
End



therevills(Posted 2015) [#3]
Also you could check the code here, it's similar to Nobuyuki:

http://www.monkey-x.com/Community/posts.php?topic=449