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?
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();
}
}
}
import greenfoot.*;
public class MyWorld extends World
{
public MyWorld()
{
super(600, 400, 1);
addObject(new Background1(), 300, 380);
addObject(new FishMan(), 300, 330);
}
}import greenfoot.*;
public class Background1 extends Actor
{
public Background1()
{
GreenfootImage img = new GreenfootImage(600, 40);
img.fill();
setImage(img);
}
}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();
}
}
}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();
}
}import greenfoot.*;
public class TitleScreen extends Actor
{
GifImage gifImage = new GifImage(“randomimage.gif”);
public void act()
{
setImage(gifImage.getCurrentImage());
}
}
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);
}
}
}
}
}