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

2013/6/10

removing objects

programmingnewb programmingnewb

2013/6/10

#
HI!!!!! I desperately need some assistance!!!! I want the thief (the player) to collect jewels. Whenever he collects a jewel, the jewel disappears and the jewel count increases by one. I have the code but it doesn't seem to work. HELP PLEASE!!!
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 = 3;
    private int health = 3;
    int currentlvl;

    boolean isCaught = false;
    boolean hasLost = false;

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

    public Thief(int a)
    {
        currentlvl = a;
    }

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

    public void act() 
    {
        setImage(rR);
        go();
        ladder();
    }    

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

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

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

    public void collectJewel()
    {
        Actor j = getOneObjectAtOffset(0, 0, Jewel.class);   
        if (isTouching(Jewel.class))
        {
            jewelCount ++;
            getWorld().removeObject(j);        
        }
    }

    public void die()
    {
        if (health == 0)
        {
            Greenfoot.setWorld(new GameOver());
        }
    }

    public void ladder()
    {
        Actor ladder;
        ladder = getOneObjectAtOffset(0, 5, Ladder.class);
        if (ladder != null)
        {
            if (Greenfoot.isKeyDown("up"))
            {
                setLocation(getX(), getY() - 3);
            }
        }
        Actor ladderd;
        ladderd = getOneObjectAtOffset(0, 42, Ladder.class);
        if (ladderd != null)
        {
            if (Greenfoot.isKeyDown("down"))
            { 
                setLocation(getX(), getY() + 3);             
            }
        }
    }

    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 void nextLvl()
    {
        if (jewelCount == 3 && isTouching(Door.class))
        {
            switch (currentlvl)
            {
                case 0:
                Greenfoot.setWorld(new StartMenu());
                break;

                case 1:
                Greenfoot.setWorld(new lvl_1());
                break;

                case 2:
                Greenfoot.setWorld(new lvl_2());
                break;

                case 3:
                Greenfoot.setWorld(new lvl_3());
                break;

                case 4:
                Greenfoot.setWorld(new lvl_4());
                break;

                case 5:
                Greenfoot.setWorld(new lvl_5());
                break;                
            }
        }
    }
}
Zamoht Zamoht

2013/6/10

#
You have to put collectJewel() in your act method.
public void act()   
    {  
        setImage(rR);  
        go();  
        ladder();  
        collectJewel();
    } 
Zamoht Zamoht

2013/6/10

#
Actually you have to put all your methods in the act() if you want the game to update and check for all those things. Your methods looks good, but they are never called because they're not in the act().
programmingnewb programmingnewb

2013/6/10

#
...I totally forgot about that!!!! LOL thank you!! I'm doing this for my high school project and I'm freaking out because it's due tomorrow and I didn't even get half done....THANKS A BUNCH!!! i can't believe i missed that.....
Zamoht Zamoht

2013/6/10

#
It's cool good luck :)
You need to login to post a reply.