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
5
6
virginia001 virginia001

2013/12/18

#
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) 
    if ( platform.getSchip() != null) 
       {  
        System.out.println("geraakt");   
        platform.getSchip().setKratten(this); // schip laadt krat in
        getWorld().removeObject(this); // 'krat verdwijnt 
       }
    
    
    private void setPlatform(Platform p)  
    {  
    platform = p;  
    }  
    public Platform getPlatform()  
    {  
    return platform;  
    }  
    } 
  }   
}  

danpost danpost

2013/12/18

#
Yeah, your bracketing was off elsewhere (not at line 27, where it appeared to be because the whole class was not given). At this point, move line 37 back up to line 27; that should fix the bracketing and a different error (if any) will be given. Then do the same with it, copy/paste the entire error and show the entire class).
danpost danpost

2013/12/18

#
I do not see the instance field for the platform in the code. At line 11, insert the following:
private Platform platform;
Also, change line 29 to this:
public void setPlatform(Platform p)
virginia001 virginia001

2013/12/18

#
adjusted exactly what you said but still the same error as before...and another little problem: my editor in greenfoot keeps being highlighted yellow and i cant get it off....here my code what i've until now
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
{
    private Platform platform;

    public void act()
   {
    
    if (Greenfoot.mouseClicked(this))  
    {  
    if (platform != null && platform.getSchip() != null) 
    if ( platform.getSchip() != null) 
       {  
        System.out.println("geraakt");   
        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/18

#
Close the Greenfoot application and re-open it; that should fix the highlighting problem. Open the Kratten class editor, remove or comment out line 18 OR 19 and then press 'Control-Shift-I'. Finally, double-check that your brackets are matching up (add and remove as necessary).
virginia001 virginia001

2013/12/18

#
the highlighting problem is gone know so ty for that.... as for the expression problem still not fixed...i checked and counted the brackets but couldn't fin something..heres the refreshed code though..
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
{
    private Platform platform;

    public void act()
    {

        if (Greenfoot.mouseClicked(this))  
        {  
            //if (platform != null && platform.getSchip() != null) 
            if ( platform.getSchip() != null) 
            {  
                System.out.println("geraakt");   
                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/18

#
The bracket to end the 'act' method is not where it should be. Without all the comments, it should like more like this:
import greenfoot.*;

public class Kratten extends Actor
{
    private Platform platform;

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

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

    public Platform getPlatform()
    {
        return platform;
    }
}
danpost danpost

2013/12/18

#
It could also be coded like this:
import greenfoot.*;

public class Kratten extends Actor
{
    private Platform platform;

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

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

    public Platform getPlatform()
    {
        return platform;
    }
}
virginia001 virginia001

2013/12/18

#
Thanks there are no more errors and the code is looking very good now....But the mmost inportant thing is that when my ship is against the platform the crate won't load on the ship even when i click on it.....My code is looking like this:
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/18

#
It is probably that your platform is not informed that a ship is 'docked' at it, so 'platform.getShip()' is returning 'null'. The Schip class act method should be checking for a platform, and when found set the Schip field of the platform to it and set its Platform field to the platform; also, any movement of the ship should reset those values depending on whether it is docked or not. Show your Schip class code.
virginia001 virginia001

2013/12/18

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


public class Schip extends Actor
{
    
    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 (getOneIntersectingObject(Platform.class) != null) moveLeft(); 
    }

    public void moveLeft()
    {
        setLocation(getX() - speed, getY());
        setRotation(0);
        if (getOneIntersectingObject(Platform.class) != null) moveRight();

    }

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

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

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

    public Kratten getKratten()  
    {  
        return kratten;  
    }   
}

        
danpost danpost

2013/12/18

#
You will need a 'private Platform platform;' field in this class, so it can 'null'ify the Schip field of the platform when moving away from it (I do not think that 'get' and 'set' methods are necessary for the 'platform' field, here). In each of the movement method blocks (lines 33 through 60), change the 'if (getOneIntersecting...' statements to the following snippet (remember, the ship is already being told to move at this point, so we clear the fields before setting them anew):
if (platform != null)
{
    platform.setShip(null);
    platform = null;
}
Platform p = (Platform)getOneIntersectingObject(Platform.class);
if (p != null)
{
    move****()// opposite movement call
    turn(180);
    platform = p;
    p.setSchip(this);
}
After doing this, we can see what we have and continue from there.
virginia001 virginia001

2013/12/18

#
i've changed the code but it still doesn't work though....
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"))
        {
            platform = null;
            moveLeft();
        } 

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

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

        if(Greenfoot.isKeyDown("down"))
        {
            platform = null;
            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)  
        {  
            // opposite movement call  
            turn(180);  
            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)  
        {  
            // opposite movement call  
            turn(180);  
            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)  
        {  
            // opposite movement call  
            turn(180);  
            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)  
        {  
            // opposite movement call  
            turn(180);  
            platform = p;  
            p.setSchip(this);  
        }  
    }

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

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

2013/12/18

#
I had edited my last post (within a minute or two of first posting it). I was hoping you would not get a chance to see the post so quickly after initially posting it. I removed the part about setting the 'platform' field to 'null' in each of the 'if' blocks when key-checking. Remove all occurances of 'platform = null;' between lines 11 and 33.
danpost danpost

2013/12/18

#
You also need to replace my comments between lines 36 and 111 with the appropriate 'move***' method call.
There are more replies on the next page.
1
2
3
4
5
6