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

7
8
9
10
11
12
13
virginia001 virginia001

2013/12/23

#
when i put this code in the truck class (Vrachtwagen) then i get an error at line setVrachtwagen that it can't find the method. and for the score adjustment i've added 3 hearts into my scenario as lives in the Lives class but i don't know how to set a conditon for it into code though..
public void act()
    {
        if (Greenfoot.mouseClicked(this))  
        {  
            if ( platform.getSchip().getKratten() != null )
            {  
                platform.getSchip().setVrachtwagen(this); // vrachtwagen laadt kratten in
                getWorld().removeObject(this); // 'krat verdwijnt 
            }
        }
    }
danpost danpost

2013/12/24

#
'platform.getSchip' has to not be null also (you need both conditions. The platform should already be setting when the ship is at the platform; so line 7 should be removed. I do not think you want the truck removed from the world at this point; so line 8 should be removed also. You should have something like this:
public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        if (platform.getSchip() != null && platform.getSchip().getKratten() != null)
        {
            Kratten crate = platform.getSchip().getKratten();
            platform.getSchip().setKratten(null);
            setPlatform(null);
            // code above should be exact
            // code below is 'something like' what you need
            if (!crate.getColor().equals(this.getColor()) // determine if colors are different
            { // lose live
                Heart heart = (Heart)getWorld().getObjects(Heart.class).get(0);
                heart.adjustLives(-1);
                // determine if no lives left
                if (heart.getLives() == 0) ((Vracht)getWorld()).gameOver();
            }
        }
    }
}
virginia001 virginia001

2013/12/24

#
at line 9 it gives the error that it cannot find the symbol method setPlatform(<nulltype>) and another thing in line 12 do you think the (!crate....etc) needs to be changed to kratten or does it needs to adjusted in another way because i think that this way it doesn't get the colors of the crates though but i could be wrong..
danpost danpost

2013/12/24

#
You put this code in the Vrachtwagen class -- did you not? and it does have a 'Platform platform' field and the standard 'get' and 'set' methods? and the subclasses of Vrachtwagen do not have any conflicting fields or methods (they should not be in the subclasses). 'crate' is a field name created locally (within the method). It is declared and set in line 7. It really does not matter what you call it; although, it should be something the tells you what the field holds. What really matters, is that it holds the correct object or value (in this case the crate (or Kratten object) that the ship was carrying (or 'null' if the ship was not carrying one). You could use the 'instanceof' keyword to determine the color of each truck, ship, or crate object, using:
if (crate instanceof groen) // checks if Kratten object is green, or
if (platform.getSchip() instanceof Schipgroen) // check if ship is green
However, instead of having to check all 6 colors this way for each type object, it would be easier to add a field to the superclasses, 'Schip', 'Kratten', and 'Vrachtwagen'. It could be an int field to hold values between 0 and 5 inclusive, or a String field to hold the name of the colors, or a Color field to hold the actual colors. At any rate, you will need 'get' and 'set' methods in these three classes and a constructor in each of their subclasses to set the value of the field indicating the color (using the 'set' method).
virginia001 virginia001

2013/12/24

#
Yes....this is the Vrachtwagen class.. i have putted my public platform (code below ) but i keep getting the message return type required between the words public and Platform @ the first line. As for the subclasses of the Vrachtwagen class there are no methods or coding in yet
public Platform()  
    {  
        GreenfootImage base = getImage();  
        GreenfootImage image = new GreenfootImage(310, 216);  
        for (int i=0; i<310; i+=72) for (int j=0; j<216; j+=72)  
            {  
                image.drawImage(base, i, j);  
        }  
        setImage(image);  
    } 
danpost danpost

2013/12/24

#
The code supplied in your last post has nothing to do with the image of the Vrachtwagen objects. All you Vrachtwagen subclasses have default images for each one that do not need to be altered in any way. In other words, you can remove this code from the Vrachtwagen class.
virginia001 virginia001

2013/12/24

#
okay until know i have this code though, because its a little bit confusing for me i'll show you how my truck class looks like..if i compile this code the error occures at setPlatform(null) that it cant find the method again
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Vrachtwagen extends Actor
{
    public void act()  
    {  
        if (Greenfoot.mouseClicked(this))  
        {  
            if (platform.getSchip() != null && platform.getSchip().getKratten() != null)  
            {  
                Kratten kratten = platform.getSchip().getKratten();  
                platform.getSchip().setKratten(null);  
                setPlatform(null);  
                // code above should be exact  
                // code below is 'something like' what you need  
                if (!kratten.getColor().equals(this.getColor())) // determine if colors are different  
                    if (crate instanceof groen) // checks if Kratten object is green, or  
                        if (platform.getSchip() instanceof Schipgroen) // check if ship is green  
                        { // lose live  
                            Lives lives = (Lives)getWorld().getObjects(Lives.class).get(0);  
                            lives.adjustLives(-1);  
                            // determine if no lives left  
                            if (lives.getLives() == 0) ((Vracht)getWorld()).gameOver();  
                }  
            }  
        }  
    }  
    private Platform platform = null;  

    private Schip schip = null;  
    public void setSchip(Schip s)
    {
        schip = s;
    }

    public Schip getSchip()  
    {  
        return schip;  
    }

    


}    
danpost danpost

2013/12/24

#
The Vrachtwagen class does not need a Schip field or its related 'get' and 'set' methods; however, the Platform field does need 'get' and 'set' methods. Then, the prepare method should be setting the 'platform' field of each Vrachtwagen object.
virginia001 virginia001

2013/12/24

#
i've corrected the problem however the scenario can't find the getColor method though
danpost danpost

2013/12/24

#
virginia001 wrote...
i've corrected the problem however the scenario can't find the getColor method though
And it will not, until you add a color field and its respective 'get' and 'set' methods. I thought I had made that clear several posts ago.
virginia001 virginia001

2013/12/24

#
Yes i understood that but i tried the 'set' and 'get' method for the Color like you said but then i found out that for this you need to make another class otherwise it gives the error that it couldn't find it though...but imma search for this creation of the color field how it needs be done exactly
virginia001 virginia001

2013/12/24

#
I've created a new class of Color because i didnt saw another though with my limitation of knowledge in greenfoot i this is what i have in my Vrachtwagen class
public void setColor(Color c)
    {
        color = c;
    }

    public Color getColor()
    {
        return color;
    }
this is what i've until now in my Color class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Color here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Color extends Actor
{
    /**
     * Act - do whatever the Color wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act(); 

    public Color()  
    { 
        {
            if (kratten instanceof groen) // checks if Kratten object is green, or  
                if (platform.getSchip() instanceof Schipgroen) // check if ship is green  
                    if(Vrachtwagen instanceof groen ); // kijk of vrachtwagen groen is  
        }
    }
}
danpost danpost

2013/12/24

#
Why do you not go with something you are more familiar with. Use String fields that will hold values like "groen", "geel", "rood", "zwart", "oranje", and "paars". Declare the field with 'private String color;' and use the methods:
public void setColor(String c)
{
    color = c:
}

public String getColor()
{
    return color;
}
Then, to compare colors, like that of the truck with that of the crate (with 'crate' already referring to that which was on the ship and the code being in the Vrachtwagen class):
if (!color.equals(crate.getColor()))
{
    // decrease lives
    // check for game over
}
// etc.
Put a constructor in each subclass of all objects that need color similar to the following, which is for the 'groen' class (subclass of Kratten):
public groen()
{
    setColor("groen");
}
virginia001 virginia001

2013/12/28

#
i've now got this in my Kratten class but gives the error cannot find symbol color
public void setColor(Color c)
    {
        color = c;
    }

    public Color getColor()
    {
        return color;
    } 
And the second at one of the subclass of my Kratten classes, at the setColor method
public class groen extends Kratten
{ 
    public groen()  
    {  
        setColor("groen");  
    }  
}
Super_Hippo Super_Hippo

2013/12/28

#
Write the following at the beginning of the class. After you import Greenfoot.
import java.awt.Color;
There are more replies on the next page.
7
8
9
10
11
12
13