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

2016/5/12

How to make ground?

aksaa002 aksaa002

2016/5/12

#
Hello i am new at greenfoot proggraming, and sorry for my bad English i am from Serbia. How to make ground and when you when you jump under ground, go to down.
public class fire extends characters
{
    private int vSpeed = 0;
    private int speed = 6;
    private int acceleration = 1;
    private int jumpStrength = 15;
    private boolean jumping = false; 
    
    public void act() 
    {
        KeyPres();
        checkFall();
    }   
    public void KeyPres()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        }
        if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
        if(Greenfoot.isKeyDown("up")&& jumping==false )
        {
            jump();
        }
    }
    public boolean onGround()
    {
        ground under = null;
        int counter = 1;
        int max = vSpeed;
        //int variance;
        while (counter <= max && under == null)
        {
            under = (ground)getOneObjectAtOffset ( 0, getImage().getHeight() / 1 + counter, ground.class);
            counter++;
        }
        // If there is a platform, correct Pengu's height so that he always lands right on the platform
        // This avoids a wierd floating effect that was present otherwise
        if (under != null)
        {
            int newY;

            newY = under.getY() - (under.getImage().getHeight()/2) - ((getImage().getHeight() / 2))-1;

            setLocation(getX(), newY);
        }
        return under != null;

    }
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
    public void jump()
    {
        jumping = true;
        vSpeed = -jumpStrength;
        fall();
    }
     public void moveRight()
    {
        setLocation(getX() + speed, getY());
    }

    public void moveLeft()
    {
        setLocation(getX() - speed, getY());
    }
    public void checkFall()
    {
        if(onGround()) {
            vSpeed = 0;
            jumping = false;
        }
        else {
            fall();
        }
    }
}
You need to login to post a reply.