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

2012/10/22

Jump Problems

StaetheWolfe StaetheWolfe

2012/10/22

#
The code compiles fine, but when I click run, my object will jump to the top of the screen, and fall to about a third from the Worlds edge, where ground is placed. Any help on how to make this work correctly and look like a realistic jump is appreciated Here is my code for the object I'm trying to make jump;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Ghost  extends Animal
{
   private static final double WALKING_SPEED = 1.0;
   private GreenfootImage image1;
   private GreenfootImage image2;
   private int speed = 7; // running speed (sideways)
   private int vSpeed = 0;
   private int acceleration = 2;
   private int jumpStrength = 12;
 
   
   
    public Ghost()
    {
       image1 = new GreenfootImage("Otis_Left.png");
       image2 = new GreenfootImage("Otis_Right.png");
    }
    
      public void act() 
    {
        controlGhost();
        checkFall();
       // Add your action code here.
    }
    public void moveBack()
        {
             double angle = Math.toRadians( getRotation() );
             int x = (int) Math.round(getX() - Math.cos(angle) * (3 * WALKING_SPEED));
             int y = (int) Math.round(getY() + Math.sin(angle) * (3 * WALKING_SPEED));
        
             setLocation(x, y);
        }
   
    /**
     * control the Ghost with arrow Keys
     */
    public void controlGhost()
    {
        if (Greenfoot.isKeyDown("right") )
            {
                move();
                setImage(image2);
            }
        if (Greenfoot.isKeyDown("left") )
            {
                moveBack();
                setImage(image1);
            }
        if (Greenfoot.isKeyDown("up") )
            {
                jump();
            }
    } 
    public void jump()
    {
        vSpeed = -jumpStrength;
        fall();
    }

    public void checkFall()
    {
        if(onGround()) 
        {
            vSpeed = 0;
        }
        else 
        {
            fall();
        }
    }

    public boolean onGround()
    {
        int myHeight = getImage().getHeight();    
        Actor under = getOneObjectAtOffset(0, myHeight/2, Ground.class);
        return under != null;
    }

    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
    
    public void moveRight()
    {
        setLocation ( getX() + speed, getY() );
    }
    
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
    }
}
danpost danpost

2012/10/22

#
The problem probably is a result of the fact that you are using 'isKeyDown' for jumping. Each act cycle that registers that the key is down, between the time you press the key and the time you release the key, jumpStrength is being added to ySpeed. In your jump() method, remove the statement 'fall();' as the next statement executed when returning to the act method will accomplish that. I believe if you check to make sure that ySpeed is zero before jumping, it will work. Change your jump() method to this
public void jump()
{
    if (onGround()) // or 'if (ySpeed == 0)'
    {
        ySpeed = -jumpStrength;
    }
}
davmac davmac

2012/10/22

#
I believe if you check to make sure that ySpeed is zero before jumping, it will work.
No, at the zenith of the jump the ySpeed will also (briefly) be equal to 0; the onGround() check is what you really want.
StaetheWolfe StaetheWolfe

2012/10/23

#
When I form the code like this (I assume adding private int ySpeed=0; to the top of the code) it compiles fine, but when I press the up arrow, nothing happens.
public void jump()
    {
            if(onGround())
            {
                ySpeed = -jumpStrength;
            }
       }
danpost danpost

2012/10/23

#
With the scenario stopped, right-click on the actor and select the 'onGround()' method to see if it returns 'true'. If not either adjust where the actor is put into the world or declare ySpeed = 1 instead of 0.
StaetheWolfe StaetheWolfe

2012/10/23

#
onGroud() returns true when the it looks like it is touching the ground. It also returns true when it is a lot higher than the ground. If I place the actor where onGround() returns false, it falls to where it recognizes ground (not where the image of ground is) and when the up arrow key is pressed, proceeds to do nothing. Changing ySpeed=1 does the same thing.
You need to login to post a reply.