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

2014/4/1

Jumping on a simple game

Nkhurshid2000 Nkhurshid2000

2014/4/1

#
Hi, i was making a simple game. After i put this code in, it jumps perfectly, however, if i hold down the space bar, my actor goes right up and when i let go, it comes back down again. Can anyone help with making the actor doing even jumps?
Nkhurshid2000 Nkhurshid2000

2014/4/1

#
THIS IS MY CODE public class Man extends Actor { private int speed = 2; //movement speed private int vSpeed = 0; //vertical speed private int acceleration = 2; //gravity effect while falling private int jumpStrength = -8; public void act() { checkKeys(); checkFall(); jump(); } public void checkKeys() { if( Greenfoot.isKeyDown("left")) { move(-5); } if(Greenfoot.isKeyDown("right")) { move(5); } if(Greenfoot.isKeyDown("up")) { jump(); } } public void jump() { if (Greenfoot.isKeyDown("space")) { vSpeed = jumpStrength; fall(); } } public void fall() { setLocation(getX(), getY()+vSpeed); vSpeed = vSpeed + acceleration; } public boolean onPlatform() { Actor under = getOneObjectAtOffset (0, getImage().getHeight()/2, Platform.class); return under != null; } public void checkFall() { if (onPlatform()) { vSpeed = 0; } else { fall(); } }
danpost danpost

2014/4/1

#
You just need one more condition placed on jumping. Change the first line in your 'jump' method to:
1
if (Greenfoot.isKeyDown("space") && onPlatform())
Nkhurshid2000 Nkhurshid2000

2014/4/1

#
IT WORKED! Thanks a lot!
You need to login to post a reply.