Hello everybody,
I need a bit help with my jump n' run game.
I managed to make the main actor jump and fall, but somehow the actor still falls inside the ground pretty often.
Sometimes it works and it stays on the ground but most of the time the actor goes inside the ground.
Actor and Blocks are all 60 px height.
Also there is a problem where the actor jumps the whole time while pressing space. Is it possible to make the actor only jump one time and then it's only possible to jump again when the actor is on the ground again?
I would really appreciate help :)
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import greenfoot.*; public class Peach extends Actor { private int vSpeed = 0 ; private int acceleration = 2 ; public void act() { checkKeys(); checkFall(); } public void checkKeys() { if (Greenfoot.isKeyDown( "left" )) { setLocation(getX()- 2 , getY()); } else if (Greenfoot.isKeyDown( "right" )) { setLocation(getX()+ 2 , getY()); } else { setLocation(getX()+ 1 , getY()); } if (Greenfoot.isKeyDown( "space" )){ jump(); } } public void checkFall(){ if (onGround()){ } else { fall(); } } public boolean onGround(){ Actor unter = getOneObjectAtOffset( 0 , 30 , Ground. class ); return unter != null ; } public void jump(){ vSpeed = - 11 ; fall(); } public void fall(){ setLocation( getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; } } |