This site requires JavaScript, please enable it in your browser!
Greenfoot back
Hiimachicken
Hiimachicken wrote ...

2014/2/9

help with jumping

Hiimachicken Hiimachicken

2014/2/9

#
does anyone have a code to jump like i press the space bar and i jump but can jeep jumping in mid air and if i hit the bottom of the screen then i die so i need to keep jumping? thanks if you can provide me with a code
danpost danpost

2014/2/9

#
This would be much easier to code than the normal jump code: (1) being on the ground does not need to be checked; (2) the vertical speed will decrease as long as the key is down; and, (3) the vertical speed will increase as long as the key is not down; which means gravity is no applied while the key is down. This makes it a simple 'if-then-else':
if (Greenfoot.isKeyDown("up")) vSpeed--; else vSpeed++;
setLocation(getX(), getY()+vSpeed);
if (getY() == getWolrld().getHeight()-1) Greenfoot.stop(); // or whatever
You need to login to post a reply.