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

2013/12/17

how to let my ship get a crate

13
14
15
16
17
18
19
virginia001 virginia001

2014/1/8

#
danpost wrote...
Let us clean up your code a little bit: - remove the Color class from the project - remove all code within the Lives class (leave the 'import' statement and the class declaration statement with its curly brackets). - do what I suggested in my last post Then we can work on the leaving of the trucks and ships, plus the spawning of new ships. Let me know when you are done with the above.
I'm done now with the loading part can we proceed with the trucks leaving and spawning the new ships...
danpost danpost

2014/1/8

#
For the trucks to roll off, we first ask what is different at this point (about a truck) that this action can now be taken. The answer is that there is a Kratten object in the 'kratten' field, not 'null', as before. Therefore, in the 'act' method of the 'Vrachtwagen' class after any code already in that method:
if (kratten != null) setLocation(getX(), getY()-1); // move up
if (getY() == 0) getWorld().removeObject(this); // get removed at top
virginia001 virginia001

2014/1/8

#
The trucks are limited to loading one crate now but how do I change it in getting multiple crates (i.e. the trucks need to move to the top when all crates of the same colors are loaded into it)
danpost danpost

2014/1/8

#
I just realized that my last code post would not work anyway (there is no kratten field for the truck objects. As long as you are limited to two crates of the same color AND you do not allow crates of one color to be loaded onto a ship of a different color (or the loading of crates of one color being loaded onto a truck of a different color -- just losing a life when these events are tried), the coding should not be too un-manageable. Please confirm that these conditions are adequate.
virginia001 virginia001

2014/1/8

#
Yes ur rightt...but i want to eventually make the crates randomly all over the platform so it also can be 3 or 4 or something like that, just that the truck understands when all the crates of the same color are removed. And the same applies for the ships but they just remove and a new ship spawnss
danpost danpost

2014/1/8

#
Alright, first we now have to change the code so that the crate is not removed immediately when clicked on, but only if it is the correct color of the ship. Your current act method of the Kratten class is something like this:
public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        if (platform.getSchip() != null && platform.getSchip().getKratten() == null)
        {
            platform.getSchip().setKratten(this);
            if (!platform.getSchip().getColor().equals(this.getColor ()))
            {
                removeLive();
            }
            getWorld().removeObject(this);
        }
    }
}
It needs adjusted to this:
public void act()
{
    if (Greenfoot.mouseClicked(this) && 
        platform.getSchip() != null && 
        platform.getSchip().getKratten() == null)
    {
        if (platform.getSchip().getColor().equals(this.getColor ()))
            loadMeOnShip();
        else
            removeLive();
    }
}
// add this method
private void loadMeOnShip()
{
    platform.getSchip().setKratten(this);
    getWorld().removeObject(this);    
}
virginia001 virginia001

2014/1/8

#
i get an error 'else' without if
virginia001 virginia001

2014/1/8

#
nvm..but we can proceed now with the trucks loading multiple crates
danpost danpost

2014/1/8

#
The next step would be to have a way for a truck to determine if it is time to depart (if any more crates of the same color still need loaded or not). The correct time for a truck to check this condition is when a crate is loaded onto it. Right now, the act method of your Vrachtwagen class looks something like this:
import greenfoot.*;

public class Vrachtwagen extends Actor
{
    public void act()  
    {  
        if (Greenfoot.mouseClicked(this))  
        {  
            if (platform != null && platform.getSchip() != null && platform.getSchip().getKratten() != null)  
            {  
                Kratten kratten = platform.getSchip().getKratten();  
                platform.getSchip().setKratten(null);  
                setPlatform(null);  
                if (!kratten.getColor().equals(this.getColor ())) removeLive();
            }
        }
    }
    private Platform platform = null;  
    public void setPlatform(Platform p)  
    {  
        platform = p;  
    }  

    public Platform getPlatform()    
    {    
        return platform;    
    }  

    private String color;
    public void setColor(String c)
    {
        color = c;
    }

    public String getColor()
    {
        return color;
    }

    public void removeLive()  
    {  
        int lives = getWorld().getObjects(Lives.class).size();  
        getWorld().removeObject((Actor)getWorld().getObjects(Lives.class).get(lives-1));
        if (lives == 1) Greenfoot.stop();  
    }  
}
It is quite messy even after removing the comments, so I will clean it up a bit with the new code:
The next step would be to have a way for a truck to determine if it is time to depart (if any more crates of the same color still need loaded or not).  Fortunately, each crate has a reference to the platform object it originated from; that will make things a bit less complicated.  Right now, the act method of your Vrachtwagen class looks something like this:
[code]import greenfoot.*;

public class Vrachtwagen extends Actor
{
    private String color;
    private Platform platform;  

    public void act()  
    {  
        if (Greenfoot.mouseClicked(this) &&
            platform != null &&
            platform.getSchip() != null &&
            platform.getSchip().getKratten() != null)  
        {  
            if (kratten.getColor().equals(this.getColor ()))
                loadCrate();
            else
                removeLive();
        }
    }

    private void loadCrate()
    {
        Kratten kratten = platform.getSchip().getKratten();  
        platform.getSchip().setKratten(null);  
        setPlatform(null);  
    }

    public void removeLive()  
    {  
        int lives = getWorld().getObjects(Lives.class).size();  
        getWorld().removeObject((Actor)getWorld().getObjects(Lives.class).get(lives-1));
        if (lives-1 == 0) Greenfoot.stop();  
    }  

    public void setPlatform(Platform p) { platform = p; }  

    public Platform getPlatform() { return platform; }  

    public void setColor(String c) { color = c; }

    public String getColor() { return color; }
}
That looks a lot better. Now we want to put a condition on changing the 'platform' field to 'null'; only to do this when no more crates of that color remain to be loaded -- changing 'setPlatform(null);' to 'if (no more to be loaded on this truck) setPlatform(null);'. The importance of the 'platform' field will become clear when we add the code for the truck pulling off and disappearing. So, we can start with the line below and continue from there:
if (canMove()) setPlatform(null);
Now, the 'canMove' method we will now add must return a boolean value indicating whether the number of crates of this truck's color is zero or not.
private boolean canMove()
{
    // must check each remaining crate
    for (Object obj : getWorld().getObjects(Kratten.class))
    {
        // cast objects to type Kratten
        Kratten crate = (Kratten) obj;
        // fail if this crate is color of truck
        if (crate.getColor().equals(color)) return false;
    }
    // if made it out of loop, no more crates of color remain
    return true;
}
Cleaning it up a little, this becomes:
private boolean canMove()
{
    for (Object obj : getWorld().getObjects(Kratten.class))
        if (((Kratten)obj).getColor().equals(color)) return false;
    return true;
}
Finally (and this will show the need for the 'platform' field), we can now move the truck out on the condition that the 'platform' field has a 'null' value. Add to the end of the act method (inside the block of code for the act method) the following:
if (platform == null)
{
    setLocation(getX(), getY()-1);
    if (getY() == 0) getWorld().removeObject(this);
}
virginia001 virginia001

2014/1/8

#
I've adjusted everything into my Vrachtwagen class but when I compile it the error 'illegal start of type' comes at the beginning of 'if (canMove()) setPlatform(null);'
danpost danpost

2014/1/8

#
That line should replace the last line of your 'loadCrate' method. If you continue with problem, post the Vrachtwagen class as it is now.
virginia001 virginia001

2014/1/8

#
I now have the error at ' if (kratten.getColor().equals(this.getColor ())) loadCrate(); ' saying cant find the symbol (redmarking kratten)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Vrachtwagen here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 *///Greenfoot.playSound("load crate.wav");
public class Vrachtwagen extends Actor
{
    private String color;  
    private Platform platform;    

    public void act()    
    {    
        if (Greenfoot.mouseClicked(this) &&  
        platform != null &&  
        platform.getSchip() != null &&  
        platform.getSchip().getKratten() != null)    
        {    
            if (kratten.getColor().equals(this.getColor ()))  
                loadCrate();  
            else  
                removeLive();  

            if (platform == null)  
            {  
                setLocation(getX(), getY()-1);  
                if (getY() == 0) getWorld().removeObject(this);  
            }  
        }  
    }  

    private void loadCrate()  
    {  
        Kratten kratten = platform.getSchip().getKratten();    
        platform.getSchip().setKratten(null);    
        setPlatform(null);
        if (canMove()) 
            setPlatform(null);
    } 

    public void removeLive()    
    {    
        int lives = getWorld().getObjects(Lives.class).size();    
        getWorld().removeObject((Actor)getWorld().getObjects(Lives.class).get(lives-1));  

        if (lives != 1)  
        {  
            //Only does the following, if it was the not the last last life  
            Greenfoot.playSound("lose life.wav");  
        }  
        else  
        {  
            //Only does the following, if it was the last life.  
            Greenfoot.playSound("game over.wav");  
            Greenfoot.stop();  
        }  
    }

    public void setPlatform(Platform p) { platform = p; }    

    public Platform getPlatform() { return platform; }    

    public void setColor(String c) { color = c; }  

    public String getColor() { return color; }  

    private boolean canMove()  
    {  
        for (Object obj : getWorld().getObjects(Kratten.class))  
            if (((Kratten)obj).getColor().equals(color)) return false;  
        return true;  
    }  
}  
danpost danpost

2014/1/8

#
Sorry, copied one too many lines into the 'loadCrate' method. Change the 'act' and 'loadCrate to this:
public void act()
{
    if (Greenfoot.mouseClicked(this) &&
    platform != null &&
    platform.getSchip() != null &&
    platform.getSchip().getKratten() != null)
    {
        Kratten kratten = platform.getSchip().getKratten();
        if (kratten.getColor().equals(this.getColor ()))
            loadCrate();
        else
            removeLive();
    }
    if (platform == null)
    {
        setLocation(getX(), getY()-1);
        if (getY() == 0) getWorld().removeObject(this);
    }
}

private void loadCrate()
{
    platform.getSchip().setKratten(null);
    if (canMove()) setPlatform(null);
}
danpost danpost

2014/1/8

#
Made another correction to the code above. Use the above as it is NOW.
virginia001 virginia001

2014/1/8

#
Yesss it works thankss.. but one tiny thing before we go to the ship spawning i want to put this line 'Greenfoot.playSound("load crate.wav");' into the scenario so that it plays a sound when i load a crate
There are more replies on the next page.
13
14
15
16
17
18
19