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

1
2
3
4
virginia001 virginia001

2013/12/17

#
Hellooo....i want to ship my crates to my truck...the first is letting my ship get the crate but i dont know how i must put it in code. My crates are standing on a platform.....i've tried some code but it didnt work, so far i have this.. public void act() { disappear(); } public void disappear(){ if(Greenfoot.mouseClicked(this)){ getWorld().removeObject(this); } }
danpost danpost

2013/12/17

#
Is the ship to carry only one crate at a time or more? Is the ship supposed to be near or touching a crate before it can be picked up? Do the crates have any state that must be retained (are all crates exactly the same in all respects or different in any way)?
virginia001 virginia001

2013/12/17

#
the ship is supposed to carry one crate.. there different types of ships and crates due to there colors. The purpose of the game is to carry the right color with the right color of the ship and bring it to the right color of the truck, so for example a red crate goes to the red ship and finally brings it to the red truck that is standing on the opposite side of the crates'platform. The ship is supposed to first hit the crates' platform to load the crate with him and the same goes for the opposite side with the truck platform.
danpost danpost

2013/12/17

#
Then the Ship class should have an instance field to hold the Crate object acquired. This field should be 'null' unless carrying the crate, in which case it should hold the crate being carried. All non-platform classes (the ship, crate and truck) should have an instance field to hold 'null' or the Platform actor it is at. That way you can make sure both object are on the same platform before moving a crate (ship and crate must be on the same platform, and of same color, for crate to be loaded on ship; and, ship and truck must be on the same platform, and of same color, for crate to be transferred to the truck.
virginia001 virginia001

2013/12/17

#
i think we misunderstood eachother when i said the ship must hit the platform...the ship don't go on the platform just standing aside but it does hits the platform like sort of leaning on it (otherwise the player can move the crate on a ship from a far distance). You will get points when u sorted the right crate with the right truck but lose a life when you didn't, so there also must be a possibility that you can put the wrong crate on the wrong truck.... I also understood your reply but i can't really imagine how i must convert it into code..
danpost danpost

2013/12/17

#
There was no misunderstanding. I realized that the ship would not be intersecting the platform, but be at it. That is why you need the instance field to hold the platform object 'hit'. To indicate (even though not actually intersecting it) that the ship is 'at' that platform. Even though the color does not have to be checked when loading/unloading crates, the actors must both be at the same platform or the crates would be able to 'jump' from one platform to a ship somewhere else (not at that platform). The coding is just a matter of controlling the value of the Platform fields of each actor. Making it 'null' during any movement and setting it to any platform object encountered.
virginia001 virginia001

2013/12/17

#
Sorry man i just cant understand how i can encode this.... I really need some example of some code..
danpost danpost

2013/12/17

#
It is just a matter of creating a link between the crate or the truck, the platform and the ship so that you can determine when it is possible to move a crate Link the crate with the platform it is on; link the platform with the ship docked by it; link the truck with the platform it is on; and link the ship with the crate it carries. The links are accomplished by adding instance fields to hold the appropriate objects.
// instance field in the crate class and the Truck class
private Platform platform = null;
// with 'get' and 'set' methods for that field
public void setPlatform(p)
{
    platform = p;
}
public Platform getPlatform()
{
    return platform;
}
// instance field in Platform class
private Ship ship = null; // with cooresponding 'get' and 'set' methods
// instance field in Ship class
private Crate crate = null; // with cooresponding 'get' and 'set' methods
Then when a ship 'docks' set 'ship' in the docking platform to the ship; and when the ship undocks, set it back to 'null'. When a crate is clicked on, check to see if a ship is docked at the platform before moving the crate.
// in crate act method or a method it calls
if (Greenfoot.mouseClicked(this))
{
    if (platform != null && platform.getShip() != null)
    {
        platform.getShip().setCrate(this); // ship gets crate
        getWorld().removeObject(this); // crate disappears
    }
}
Other adjustments to the fields must be made at the appropriate times. When ship moves, when crate is unloaded and when crate is loaded on truck. Checks like that in the above will aid in getting the correct behavior for your scenario.
virginia001 virginia001

2013/12/17

#
should do the same as setPlatform(p) { platform = p; } for the crate and ship class because when i do that,for example setKratten(k) they give me an error
danpost danpost

2013/12/17

#
What error (copy/paste)? what code (show class code using 'code' tag below 'Post a reply' box)?
virginia001 virginia001

2013/12/17

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

/**
 * Write a description of class Kratten here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Kratten extends Actor
{
    /**
     * Act - do whatever the Kratten wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()  
    {
    if (Greenfoot.mouseClicked(this))  
     {  
    if (platform != null && platform.getSchip() != null)  
       {  
        platform.getSchip().setKratten(this); // ship gets crate  
        getWorld().removeObject(this); // crate disappears  
       }  
     }
    }
    private Kratten kratten = null; // with cooresponding 'get' and 'set' methods  
   
   
    
   public Kratten getKratten()  
    {  
        return kratten;  
    }  
    
   private Platform platform = null;  
// with 'get' and 'set' methods for that field  
   public void setPlatform(p)  
   {  
    platform = p;  
   }  
   public Platform getPlatform()  
   {  
    return platform;  
   }  
}    
virginia001 virginia001

2013/12/17

#
so for example when i type in the code: public void setPlatform(p) { platform = p; } public Platform getPlatform() { return platform; } they give me the error <identifier> expected in (p(here)) at setPlatform
Super_Hippo Super_Hippo

2013/12/17

#
public void setPlatform(int p)  //you need an indentifier here, for example "int". I guess, dan forgot it, but I could be wrong.
   {  
    platform = p;  
   }  
   public Platform getPlatform()  
   {  
    return platform;  
   }  
virginia001 virginia001

2013/12/18

#
okay thanks for that part...but i got a new problem now; There's given another error at the code shown below. at the setKratten(this); it gives the error: method setKratten in class Schip cannot be applied to given types; required:int; found:kratten;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Kratten here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Kratten extends Actor
{
    /**
     * Act - do whatever the Kratten wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()  
    {
    if (Greenfoot.mouseClicked(this))  
     {  
    if (platform != null && platform.getSchip() != null)  
       {  
        platform.getSchip().setKratten(this); // ship gets crate  
        getWorld().removeObject(this); // crate disappears  
       }  
     }
    }
    private Kratten kratten = null; // with cooresponding 'get' and 'set' methods  
   
   
    
   public Kratten getKratten()  
    {  
        return kratten;  
    }  
    
   private Platform platform = null;  
// with 'get' and 'set' methods for that field  
   public void setPlatform(int p)  
   {  
    platform = p;  
   }  
   public Platform getPlatform()  
   {  
    return platform;  
   }  
}    
danpost danpost

2013/12/18

#
Sorry was out for a bit. And I see my missed parameter typing. Like in line 37 of the Kratten class, the method header should be:
private void setPlatform(Platform p)
We need to tell the compiler what type of data is to be passed to the method when called (in this case 'p' is a 'Platform' object). All the other set methods should also show what type of data is being passed.
There are more replies on the next page.
1
2
3
4