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

11
12
13
14
15
16
17
danpost danpost

2014/1/3

#
You need to call 'removeLive' at line 23 of your Vrachtwagen class ( after 'if (!kratten.getColor().equals(color)) {' ).
virginia001 virginia001

2014/1/3

#
I have now this is in my Vrachtwagen class but the only problem is that it removes a life everytime it loaded a crate on a truck no matter it's the good color or not
if (!kratten.getColor().equals(this.getColor ())) // bepaal of kleuren gelijk zijn 
                {
                    removeLive();
                }  
virginia001 virginia001

2014/1/4

#
is it possible that between the lines 1 and 2 there must be an 'else' statement
Super_Hippo Super_Hippo

2014/1/4

#
That is what the '!' does. Maybe something is wrong with the 'getColor()' method.
danpost danpost

2014/1/4

#
The exclamation point ( '!' ) negates the resulting Boolean. So, if the color of the crate equals the color of this truck, the condition will return 'false' when negated. If the colors are not equal, the condition will return as 'true' when negated. You read it as 'if the color of the crate is NOT equal to the color of this truck'. Therefore, 'removeLive' should only be executed when the colors do not match. Please show the current code for the Vrachtwagen class.
virginia001 virginia001

2014/1/4

#
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)
 */
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 ())) // bepaal of kleuren gelijk zijn 
                {
                    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();  
    }  
}
danpost danpost

2014/1/4

#
The problem is not in this code of this class. Did you put constructors in the subclasses of the Vrachtwagen class to set the String color of the trucks (like you did for the subclasses of Kratten)?
virginia001 virginia001

2014/1/4

#
no i don't have any code in my Vrachtwagen subclasses but maybe you can look in my scenario to look whats wrong in the code because i can't see any mistakes..
danpost danpost

2014/1/4

#
virginia001 wrote...
no i don't have any code in my Vrachtwagen subclasses but maybe you can look in my scenario to look whats wrong in the code because i can't see any mistakes..
You should have a constructor in each subclass to set the color field to the appropriate color with. This is just like what we did with the Kratten subclasses.
virginia001 virginia001

2014/1/4

#
And the same is also needed voor the ship class right?? i have this now in my Vrachtwagengroen subclass:
public VrachtwagenGroen()  
    {  
        setColor("groen");  
    }  
and this in my Schipgroen class
public Schipgroen()  
    {  
        setColor("groen");  
    }  
virginia001 virginia001

2014/1/5

#
I've added the subclasses for my ships,crates and trucks but the lifes are now only dissapearing when loading a wrong crate on the wrong truck and not immediatly aftyer i load on a ship (i.e. yellow crate on green ship) there's also need to be a life removed directly
danpost danpost

2014/1/5

#
That code has not yet been added (or implemented). Do similar to the code of the Vrachtwagen to remove a Lives object and check for no more in the Kratten class when loading a wrong-colored crate on the ship.
virginia001 virginia001

2014/1/5

#
I already have that in my Vrachtwagen class
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)
 */
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 ())) // bepaal of kleuren gelijk zijn 
                {
                    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();  
    }  
}
danpost danpost

2014/1/6

#
You need to add something similar to lines 13 through 26 into the act method of the Kratten class.
public void act()
{
    if (Greenfoot.mouseClicked(this) && platform.getSchip() != null && platform.getSchip().getKratten() == null)  
    {  
        platform.getSchip().setKratten(this);  
        if (!platform.getSchip().getColor().equals(this.getColor ()))
        {
            removeLive();
        }
        getWorld().removeObject(this);
    }
}

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();
}
virginia001 virginia001

2014/1/6

#
I've implented this code in my Kratten class but it now gives me the error in the terminal window: java.lang.NullPointerException
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Kratten extends Actor
{
    private Platform platform;

    public void act()
    {  
        if (Greenfoot.mouseClicked(this) && platform.getSchip() != null && platform.getSchip().getKratten() == null)    
        {    
            platform.getSchip().setKratten(this);    
            if (!platform.getSchip().getColor().equals(this.getColor ()))  
            {  
                removeLive();  
            }  
            getWorld().removeObject(this);  
        }  
    }  

    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();  
    }  
}
There are more replies on the next page.
11
12
13
14
15
16
17