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

2016/12/26

jumping

Hondrok Hondrok

2016/12/26

#
Soo... I'm trying to make an Actor stay on the ground and it doesn't work
import greenfoot.*;
  
public class Dolphin extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    
    public void act() 
    {
       checkKeys();
       checkFall();
    }
    
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
        {
           moveLeft();
        } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
    }    
    
    public void checkFall()
    {
        if(onGround())
        {
           vSpeed=0; 
        }
        else
        {
            fall();
        }
    }
    
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getX() / 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());
    }
}
If somebody could help me I'd be sooo grateful :D
danpost danpost

2016/12/26

#
In line 41, should not the vertical offset use 'getY' -- not 'getX'?
Hondrok Hondrok

2016/12/26

#
danpost wrote...
In line 41, should not the vertical offset use 'getY' -- not 'getX'?
It wooorks :D thank you man
danpost danpost

2016/12/26

#
danpost wrote...
In line 41, should not the vertical offset use 'getY' -- not 'getX'?
Actually, it should probably be 'getImage().getHeight()/2'.
Hondrok Hondrok

2016/12/27

#
danpost wrote...
danpost wrote...
In line 41, should not the vertical offset use 'getY' -- not 'getX'?
Actually, it should probably be 'getImage().getHeight()/2'.
I tried with getHeight()/2 and it doesn't work. It says that it can't be found. I also used getY() and my actor floats. It doesn't stay right on the ground. Now, I don't know what to do xD
danpost danpost

2016/12/27

#
Hondrok wrote...
I tried with getHeight()/2 and it doesn't work. It says that it can't be found.
That is not what I said -- I suggested this:
getImage().getHeight()/2
Hondrok Hondrok

2016/12/27

#
danpost wrote...
Hondrok wrote...
I tried with getHeight()/2 and it doesn't work. It says that it can't be found.
That is not what I said -- I suggested this:
getImage().getHeight()/2
Oh, it works! Thank you man :D
You need to login to post a reply.