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

5
6
7
8
9
10
11
virginia001 virginia001

2013/12/22

#
okay...donee
danpost danpost

2013/12/22

#
Is it working ok now? If not explain, give any errors and supply code.
virginia001 virginia001

2013/12/22

#
Still not working but no errors though... here's my crate class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Kratten extends Actor
{
    private Platform platform;

    public void act()
    {

        if (Greenfoot.mouseClicked(this))  
        {  
            //if (platform != null && platform.getSchip() != null) 
            if ( platform.getSchip() != null) 
            {  
                platform.getSchip().setKratten(this); // schip laadt krat in
                getWorld().removeObject(this); // 'krat verdwijnt 
            }
        }
    }

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

    public Platform getPlatform()  
    {  
        return platform;  
    }

}
danpost danpost

2013/12/22

#
What world class code do you have for the platforms and crates now?
virginia001 virginia001

2013/12/22

#
platform.setLocation(390, 651);
        vrachtwagenplek.setLocation(381, 53);
        rood.setPlatform(platform);
        groen.setPlatform(platform);
        oranje.setPlatform(platform);
        paars.setPlatform(platform);
        rood2.setPlatform(platform);
        zwart.setPlatform(platform);
        oranje2.setPlatform(platform);
        geel.setPlatform(platform);
        paars2.setPlatform(platform);
        geel2.setPlatform(platform);
        zwart2.setPlatform(platform);
        groen2.setPlatform(platform);
danpost danpost

2013/12/22

#
Where is it not working? is it still not picking up crates when the ship is docked and the crate is clicked on?
virginia001 virginia001

2013/12/22

#
Yeppp..still won't pick up the crates when clicking on it
danpost danpost

2013/12/22

#
This probably will not fix the problem, but the Platform class needs adjustment to:
import greenfoot.*;

public class Kratten extends Actor
{
    private Platform platform;

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

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

    public Platform getPlatform()  
    {  
        return platform;  
    }
}
I added the condition (on line 11) that the ship cannot already have a crate. To troubleshoot the problem, run the scenario to the point where a crate should be picked up, click on a crate and stop the scenario. Right-click on the crate you tried to pick up and select 'Inspect' to see if the 'platform' field is not 'null'. Right-click on the platform and inspect to see if the 'ship' field is not 'null'. Right-click on the ship and inspect to see if the 'platform' field is not 'null' and that the 'platform' field is 'null'. Report results.
virginia001 virginia001

2013/12/22

#
The green crate just gives an arrow at private Platform platform, and when i inspect the platform i see the platform Platform object inspector... it says private Schip schip = null, and finally the ship object inspector tells me that private Platform platform is alsoo null
danpost danpost

2013/12/22

#
That should tell you that the setting of the 'ship' field of the platform and the setting of the 'platform' field of the ship is not being executed for some reason. That code, I believe, is in your Schip class (your ship docking procedure).
virginia001 virginia001

2013/12/22

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

public class Schip extends Actor
{

    private Platform platform = null;
    private int speed = 3;

    public void act() 
    {
        if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        } 

        if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }

        if(Greenfoot.isKeyDown("up"))
        {
            moveUp();
        } 

        if(Greenfoot.isKeyDown("down"))
        {
            moveDown();
        } 
    } 

    public void moveRight()  
    {  
        setLocation(getX() + speed, getY());  
        setRotation(180);  
        if (platform != null)    
        {    
            platform.setSchip(null);    
            platform = null;    
        }    
        Platform p = (Platform)getOneIntersectingObject(Platform.class);    
        if (p != null)    
        {    
            setLocation(getX() - speed, getY());   
            platform = p;    
            p.setSchip(this);    
        }     
    }  

    public void moveLeft()
    {
        setLocation(getX() - speed, getY());  
        setRotation(0);  
        if (platform != null)    
        {    
            platform.setSchip(null);    
            platform = null;    
        }    
        Platform p = (Platform)getOneIntersectingObject(Platform.class);    
        if (p != null)    
        {    
            setLocation(getX() + speed, getY());   
            platform = p;    
            p.setSchip(this);    
        }     

    }

    public void moveUp()
    {
        setLocation(getX(),  getY() - speed);
        setRotation(90);
        if (platform != null)    
        {    
            platform.setSchip(null);    
            platform = null;    
        }    
        Platform p = (Platform)getOneIntersectingObject(Platform.class);    
        if (p != null)    
        {    
            setLocation(getX(), getY() + speed);   
            platform = p;    
            p.setSchip(this);    
        }  
    }

    public void moveDown()
    {
        setLocation(getX(),  getY() + speed);
        setRotation(270);
        if (platform != null)    
        {    
            platform.setSchip(null);    
            platform = null;    
        }    
        Platform p = (Platform)getOneIntersectingObject(Platform.class);    
        if (p != null)    
        {    
            setLocation(getX(), getY() - speed);   
            platform = p;    
            p.setSchip(this);    
        }  
    }

    public Kratten kratten = null;  
    public void setKratten(Kratten k)
    {
        kratten = k;
    }

    public Kratten getKratten()  
    {  
        return kratten;  
    }
}
Super_Hippo Super_Hippo

2013/12/22

#
Maybe it would be easier to help if you could upload the scenario as it is at the moment. I wonder why line 105 is public by the way.
danpost danpost

2013/12/22

#
Are you actually moving the ship (using the arrow keys) to the platform before trying to load a crate? I am kind of grasping at straws at this point; I know that the ship is added into the world near the truck platform (at least, it was in the version I downloaded. Try adding after each 'move***' call in the act method (lines 13, 18, 23, and 28) the following:
if (platform != null) System.out.println("Platform was set");
Compile the project and run it. If the terminal window does not display the message ("Platform was set"), then we can confirm that the problem is definitely in setting the fields.
virginia001 virginia001

2013/12/22

#
the ships need to be moved by the arrow keys and the ship is standing right of the truckplatform by default...as for, if (platform != null) System.out.println("Platform was set"); i have putted it into the movement acts but no terminal window popped out.
danpost danpost

2013/12/22

#
I cannot see why it is not setting the fields. The code does not seem amiss in the Kratten class or in the Schip class. Can you upload the scenario on the site, so I can see exactly what you have at this point, please.
There are more replies on the next page.
5
6
7
8
9
10
11