Hello everyone,
I'm having a problem with making a game in Greenfoot. Whenever I hit the jump button, my character shoots straight up to the edge of the world. Any suggestions that would help me fix this problem?


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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class FishMan here. * * @author (your name) * @version (a version number or a date) */ public class FishMan extends Actor { private static final int jumpStrength = 16 ; private int VSpeed = 0 ; private static final int acceleration = 2 ; private static final int speed = 7 ; /** * Act - do whatever the FishMan wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { handleMovement(); checkFall(); } public void setVSpeed( int speed) { VSpeed = speed; } public void moveLeft() { setLocation(getX()-speed, getY()); } public void moveRight() { setLocation(getX()+speed, getY()); } public boolean onGround() { Object under = getOneObjectAtOffset( 0 , getImage().getHeight()/ 2 , Background1. class ); return under != null ; } public void fall() { setLocation(getX(), getY()+VSpeed); VSpeed = VSpeed - acceleration; } private boolean atBottom() { return getY() >= getWorld().getHeight() - 2 ; } private void handleMovement() { if (Greenfoot.isKeyDown( "left" )) { moveLeft(); } if (Greenfoot.isKeyDown( "right" )) { moveRight(); } if (Greenfoot.isKeyDown( "up" )) { if (onGround()) jump(); } } private void jump() { setVSpeed(-jumpStrength); fall(); } private void checkFall() { if (onGround()) { setVSpeed( 0 ); } else { fall(); } } } |
1 2 3 4 5 6 7 8 9 10 11 | import greenfoot.*; public class MyWorld extends World { public MyWorld() { super ( 600 , 400 , 1 ); addObject( new Background1(), 300 , 380 ); addObject( new FishMan(), 300 , 330 ); } } |
1 2 3 4 5 6 7 8 9 10 11 | import greenfoot.*; public class Background1 extends Actor { public Background1() { GreenfootImage img = new GreenfootImage( 600 , 40 ); img.fill(); setImage(img); } } |
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | import greenfoot.*; public class FishMan extends Actor { private static final int jumpStrength = 16 ; private int VSpeed = 0 ; private static final int acceleration = 2 ; private static final int speed = 7 ; public void act() { handleMovement(); checkFall(); } public void setVSpeed( int speed) { VSpeed = speed; } public void moveLeft() { setLocation(getX()-speed, getY()); } public void moveRight() { setLocation(getX()+speed, getY()); } public boolean onGround() { Object under = getOneObjectAtOffset( 0 , getImage().getHeight()/ 2 , Background1. class ); return under != null ; } public void fall() { setLocation(getX(), getY()+VSpeed); VSpeed = VSpeed + acceleration; } private boolean atBottom() { return getY() >= getWorld().getHeight() - 2 ; } private void handleMovement() { if (Greenfoot.isKeyDown( "left" )) { moveLeft(); } if (Greenfoot.isKeyDown( "right" )) { moveRight(); } if (Greenfoot.isKeyDown( "up" )) { if (onGround()) jump(); } } private void jump() { setVSpeed(-jumpStrength); fall(); } private void checkFall() { if (onGround()) { setVSpeed( 0 ); } else { fall(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import greenfoot.*; public class MyWorld extends World private void prepare() { FishMan fish = new FishMan(); addObject(fish, 100 , 309 ); Background1 background = new Background1(); addObject(background, 300 , 50 ); TitleScreen title = new TitleScreen(); addObject(title, 300 , 200 ); public MyWorld() { super ( 600 , 400 , 1 ); prepare(); } } |
1 2 3 4 5 6 7 8 9 10 | import greenfoot.*; public class TitleScreen extends Actor { GifImage gifImage = new GifImage(“randomimage.gif”); public void act() { setImage(gifImage.getCurrentImage()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import greenfoot.*; public class Background1 extends Actor { public void act() { if (Greenfoot.isKeyDown(“space”)) { setLocation( 300 , 50 ); if (isTouching(TitleScreen. class )) { removeTouching(TitleScreen. class ); if (isTouching(FishMan. class )) { getWorld().addObject( new FishMan, 100 , 309 ); } } } } } |