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

2016/9/26

Object jump

nush99 nush99

2016/9/26

#
Hi.. I need help with trying to get my object (a man) to jump.. Right now I have the code to make him jump, but this makes him fall to the bottom of the world and jump from there. I want him to be able to jump from whichever point he is at in the world. Here is the code I have so far: public void act() { int groundLevel = getWorld().getHeight() - getImage().getHeight()/2; boolean onGround = (getY() == groundLevel); if (!onGround) // in middle of jump { ySpeed++; // adds gravity effect setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster) if (getY()>=groundLevel) // has landed (reached ground level) { setLocation(getX(), groundLevel); // set on ground Greenfoot.getKey(); // clears any key pressed during jump } } else // on ground { if ("space".equals(Greenfoot.getKey())) // jump key detected { ySpeed = -15; // add jump speed setLocation(getX(), getY()+ySpeed); // leave ground } } }
danpost danpost

2016/9/26

#
If you remove the 'else' line and the squiggly brackets for that 'else' block, it should jump even from in mid-air. EDIT: That is not exactly correct. You need to remove the 'Greenfoot.getKey(); // clears any key pressed during jump' line as well.
nush99 nush99

2016/9/27

#
thank you so much!!
You need to login to post a reply.