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

2014/4/13

Walking on the platform

Arthur114 Arthur114

2014/4/13

#
I need a help with my wombat game, he cant walk on the platform and i cant find out why i was trying to use many different codes all he can is ( not to jump on the platform which is about him) but i want to him to jump on the platform and walk on it.. Help me to put something into code so he can walk on the platforms
import greenfoot.*;
  
public class vilkas1 extends Actor  
{  
    private GreenfootImage image1 = new GreenfootImage("wombat.png");
    private GreenfootImage image2 = new GreenfootImage ("wombat-left.png");  
    private int gravity;
    private int ySpeed;  
     private int vSpeed = 0;
     private int aSpeed = 2;
     private int acceleration = 2;
     private int jumpStrength = 16;
  
    public vilkas1()  
    {  
    }  
  
    public void act()  
    {  
           animate();
          platformAbove();
           if(Greenfoot.isKeyDown("a")){
            move(-5);
        }
        
        if(Greenfoot.isKeyDown("d")){
            move(5);
        } 
        int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;  
        boolean onGround = (getY() == groundLevel);  
        if (!onGround) // in middle of jump  
        {  
            ySpeed++; // adds gravity effect  
            setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)  
            if (getY()>=groundLevel) // has landed (reached ground level)  
            {  
                setLocation(getX(), groundLevel); // set on ground  
                Greenfoot.getKey(); // clears any key pressed during jump  
           }  
        }  
        else // on ground  
        {  
            if ("space".equals(Greenfoot.getKey())) // jump key detected  
            {  
                ySpeed = -20; // add jump speed  
                setLocation(getX(), getY()+ySpeed);
            }  
        }  
    }  
     public void animate()
    {
        if((Greenfoot.isKeyDown("d"))){
        setImage (image1);
    }
        if((Greenfoot.isKeyDown("a"))){
            setImage (image2);
        }
    }
   public boolean platformAbove()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/-2);
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Platform.class);
        if(ceiling != null)
        {
            vSpeed = 1;
            bopHead(ceiling);
            return true;
        }
        else
        {
            return false;
        }
    }
    public void bopHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
    }
    public boolean onGround()
{
    int spriteHeight = getImage().getHeight();
    int yDistance = (int) (spriteHeight / 2);
    Actor ground = getOneObjectAtOffset (0, yDistance , Platform.class);
    if(ground == null)
    {
        return false ;
    }
    else
    {
        moveToGround(ground);
        return true;
    }
}
public void fall() 
{
    setLocation(getX(),getY()+ vSpeed);
    vSpeed = vSpeed + acceleration;
}
   public void checkFall() 
    {
        if(onGround())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
   public void moveToGround(Actor ground)
   {
       int groundHeight = ground.getImage().getHeight();
       int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
       setLocation(getX(), newY);
    }
}
You need to login to post a reply.