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

2013/6/4

How to walk on platforms & make new levels

programmingnewb programmingnewb

2013/6/4

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Thief here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Thief extends Actor
{
    public int jewelCount = 0;
    private int speed = 4;
    private int vspeed = -50;
    private int health = 3;

    boolean isCaught = false;
    boolean hasLost = false;

    private GreenfootImage rR = new GreenfootImage("robberRight.png");
    private GreenfootImage rL = new GreenfootImage("robberLeft.png");

    /**
     * Act - do whatever the Thief wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public Thief()
    {

    }

    public void act() 
    {

        go();
        jump();    
    }    

    public void go()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            setImage(rR);
            setLocation(getX() + speed, getY());
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setImage(rL);
            setLocation(getX() - speed, getY());
        }
        if (Greenfoot.isKeyDown("space"))
        {
            jump();
        }
    }

    public void eatCoin()
    {
        Actor coin;
        coin = getOneObjectAtOffset(0, 0, Coin.class);
        if (coin != null)
        {
            World world;
            world = getWorld();
            world.removeObject(coin);
        }
    }

    public void jump()
    {
        setLocation(getX(), getY() + vspeed);
        fall();
        checkFall();
    }

    public boolean onFloor()
    {
        Actor below = getOneObjectAtOffset (0, getImage().getHeight()/2, Platform.class);  
        return below != null; 
    }

    public void checkFall()
    {
        if (onFloor())
        {
            Actor below = getOneObjectAtOffset (0, getImage().getHeight()/2, Platform.class);
            setLocation(below.getX(), below.getY());
        }
        else
        {

        }
    }

    public void fall()
    {
        if (!onFloor())
        {
            setLocation(getX(), getY() - vspeed);
        }
    }

    public void collectJewel()
    {
        Actor jewel;
        jewel = getOneObjectAtOffset(0, 0, Jewel.class);
        if (jewel != null)
        {
            World world;
            world = getWorld();
            world.removeObject(jewel);
            jewelCount++;
        }
    }

    public void die()
    {
        if (health == 0)
        {
            hasLost = true;
        }
    }

    public void climbStair()
    {
        Actor ladder;
        ladder = getOneObjectAtOffset(0, 0, Ladder.class);
        if (ladder != null)
        {
            if (Greenfoot.isKeyDown("up"))
            {
                setLocation(getX(), getY() - 10);
                setLocation(getX(), getY() - 10);
                setLocation(getX(), getY() - 10);
            }
        }   
    }

    public boolean getCaught()
    {
        Actor po;
        po = getOneObjectAtOffset(0, 0, PoliceOfficer.class);
        if (po != null)
        {
            isCaught = true;
        }
        return isCaught;
    }

    public void loseHealth()
    {
        if (isCaught)
        {
            health--;
        }
    }

    public int getHealth()
    {
        return health;
    }
}
This is all I have for now but I was wondering how to make my actor walk on platforms or even jump on them. Also, my actor won't jump. Please help!!!!
uanimal uanimal

2013/6/6

#
you vspeed needs to increase when falling and decrease when jumping so put in fall vSpeed += vSpeed and jump vSpeed -= vSpeed
You need to login to post a reply.