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

2013/12/18

#
k adjusted it....
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)  
        {  
            moveLeft(); 
            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)  
        {  
            moveRight();  
            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)  
        {  
            moveDown(); 
            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)  
        {  
            moveUp();  
            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

#
Is there still a continuing problem or a new one? If so, error message and class code (if not that above).
virginia001 virginia001

2013/12/18

#
Ye still doesn't get the crates , but no errors though
danpost danpost

2013/12/18

#
When creating and adding the Kratten objects into the world, are you setting their 'platform' fields to the Platform object you are placing them on by using:
Kratten k = new Kratten();
k.setPlatform(p);
where 'p' refers to the Platform object you are placing the crate on?
virginia001 virginia001

2013/12/18

#
I dont know what you mean, but you wanna see a class??
danpost danpost

2013/12/18

#
virginia001 wrote...
I dont know what you mean, but you wanna see a class??
Yes, your world class.
virginia001 virginia001

2013/12/18

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

public class Vracht extends World
{


    public Vracht()
    {    
        super(760,760, 1); 

        prepare();
    }

    private void prepare()
    {

        Platform platform = new Platform();
        addObject(platform, 280, 730);
        Platform platform2 = new Platform();
        addObject(platform2, 300, 730);
        Platform platform3 = new Platform();
        addObject(platform3, 320, 730);
        Platform platform4 = new Platform();
        addObject(platform4, 402, 710);
        Platform platform5 = new Platform();
        addObject(platform5, 544, 711);
        Platform platform6 = new Platform();
        addObject(platform6, 527, 615);
        Platform platform7 = new Platform();
        addObject(platform7, 363, 538);
        Platform platform8 = new Platform();
        addObject(platform8, 355, 683);
        Platform platform9 = new Platform();
        addObject(platform9, 290, 588);
        Platform platform10 = new Platform();
        addObject(platform10, 371, 587);
        Platform platform11 = new Platform();
        addObject(platform11, 456, 584);
        Platform platform12 = new Platform();
        addObject(platform12, 543, 590);

        vrachtwagenplek vrachtwagenplek = new vrachtwagenplek();
        addObject(vrachtwagenplek, 275, 60);
        vrachtwagenplek vrachtwagenplek2 = new vrachtwagenplek();
        addObject(vrachtwagenplek2, 402, 67);
        vrachtwagenplek vrachtwagenplek3 = new vrachtwagenplek();
        addObject(vrachtwagenplek3, 540, 75);
        vrachtwagenplek vrachtwagenplek4 = new vrachtwagenplek();
        addObject(vrachtwagenplek4, 282, 116);
        vrachtwagenplek vrachtwagenplek5 = new vrachtwagenplek();
        addObject(vrachtwagenplek5, 427, 129);
        vrachtwagenplek vrachtwagenplek6 = new vrachtwagenplek();
        addObject(vrachtwagenplek6, 571, 136);

        platform.setLocation(283, 729);
        platform2.setLocation(355, 729);
        platform3.setLocation(427, 729);
        platform5.setLocation(499, 729);
        platform8.setLocation(283, 657);
        platform4.setLocation(355, 657);
        platform7.setLocation(427, 657);
        platform6.setLocation(499, 657);
        platform9.setLocation(283, 585);
        platform10.setLocation(355, 585);
        platform11.setLocation(427, 585);
        platform12.setLocation(499, 585);

        vrachtwagenplek.setLocation(275, 53);
        vrachtwagenplek2.setLocation(382, 53);
        vrachtwagenplek3.setLocation(489, 53);
        vrachtwagenplek4.setLocation(274, 125);
        vrachtwagenplek5.setLocation(381, 125);
        vrachtwagenplek6.setLocation(488, 125);

        VrachtwagenZwart vrachtwagenzwart = new VrachtwagenZwart();
        addObject(vrachtwagenzwart, 269, 122);
        VrachtwagenGroen vrachtwagengroen = new VrachtwagenGroen();
        addObject(vrachtwagengroen, 342, 125);
        VrachtwagenOranje vrachtwagenoranje = new VrachtwagenOranje();
        addObject(vrachtwagenoranje, 415, 123);
        VrachtwagenRood vrachtwagenrood = new VrachtwagenRood();
        addObject(vrachtwagenrood, 513, 127);
        VrachtwagenGeel vrachtwagengeel = new VrachtwagenGeel();
        addObject(vrachtwagengeel, 467, 119);

        vrachtwagengroen.setLocation(319, 121);
        vrachtwagenoranje.setLocation(388, 121);
        vrachtwagengeel.setLocation(454, 120);
        vrachtwagenrood.setLocation(514, 119);
        vrachtwagenzwart.setLocation(256, 122);
        groen groen = new groen();
        addObject(groen, 296, 596);
        groen groen2 = new groen();
        addObject(groen2, 502, 734);
        groen.setLocation(287, 590);
        groen2.setLocation(496, 727);
        oranje oranje = new oranje();
        addObject(oranje, 353, 596);
        oranje.setLocation(351, 590);
        oranje oranje2 = new oranje();
        addObject(oranje2, 291, 729);
        oranje2.setLocation(288, 726);
        paars paars = new paars();
        addObject(paars, 411, 667);
        paars.setLocation(412, 589);
        rood rood = new rood();
        addObject(rood, 502, 594);
        rood rood2 = new rood();
        addObject(rood2, 294, 664);
        rood2.setLocation(288, 661);
        paars.setLocation(426, 589);
        platform10.setLocation(355, 585);
        oranje.setLocation(356, 590);
        oranje.setLocation(358, 589);
        rood.setLocation(495, 589);
        zwart zwart = new zwart();
        addObject(zwart, 366, 665);
        zwart.setLocation(360, 660);
        zwart zwart2 = new zwart();
        addObject(zwart2, 436, 734);
        zwart2.setLocation(427, 728);
        geel geel = new geel();
        addObject(geel, 496, 660);
        geel geel2 = new geel();
        addObject(geel2, 361, 728);
        geel2.setLocation(359, 727);
        zwart.setLocation(359, 661);
        oranje2.setLocation(427, 663);
        paars paars2 = new paars();
        addObject(paars2, 294, 729);
        paars2.setLocation(288, 727);
        vrachtwagenzwart.setLocation(248, 122);
        vrachtwagengroen.setLocation(297, 121);
        vrachtwagenoranje.setLocation(358, 121);
        vrachtwagengeel.setLocation(421, 120);
        vrachtwagenrood.setLocation(476, 120);
        VrachtwagenPaars vrachtwagenpaars = new VrachtwagenPaars();
        addObject(vrachtwagenpaars, 525, 125);
        vrachtwagenpaars.setLocation(522, 120);
        vrachtwagenoranje.setLocation(350, 121);
        vrachtwagengeel.setLocation(407, 121);
        vrachtwagenrood.setLocation(468, 120);
        vrachtwagenrood.setLocation(467, 120);
        vrachtwagenrood.setLocation(461, 120);
        vrachtwagenpaars.setLocation(515, 120);

        Schipgroen schipgroen = new Schipgroen();
        addObject(schipgroen, 637, 96);
    }
}
danpost danpost

2013/12/18

#
I do not see where your Kratten objects are being added into the world /???/. Also, I noticed that you are using a set of 4 platform objects to create each of your 'full' platforms. This will cause problems as far as one part of the platform not knowing that the ship is 'docked' at another part of that same platform (cause it is not the same platform object). To fix this problem, you need to create an image of the full platform and only use one platform actor for each platform. Please show your Platform class code so we can rectify this.
virginia001 virginia001

2013/12/18

#
zwart,oranje,paars,groen,rood,geel are the Kratten subclasses, these are different colors of the crates. as for the Platform class this is the code of it:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    }

    private Schip schip = null;  

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

    public Schip getSchip()  
    {  
        return schip;  
    }  

}
danpost danpost

2013/12/18

#
Add the following method (constructor) to the Platform class:
public Platform()
{
    GreenfootImage pImg = new GreenfootImage(getImage());
    GreenfootImage image = new GreenfootImage(pImg.getWidth()*4, pImg.getHeight());
    for (int n=0; n<4; n++)
    {
        image.drawImage(pImg, pImg.getWidth()*n, 0);
    }
    setImage(image);
}
Now a single platform will be equivalent to the four original platform objects side by side, except that the whole span of the platform will 'know' when the ship is docked or not. Next, we need to adjust the 'prepare' method of your Vracht class to accommodate the change. Remove lines 55 through 66 and replace lines 17 through 40 with the following:
Platform platform = new Platform();
addObject(platform, 391, 729);
Platform platform2 = new Platform();
addObject(platform2, 391, 657);
Platform platform3 = new Platform()
addObject(platform3, 391, 585);
After doing this test the scenario; it should not show any difference from before. If it does describe how it is different. Then we will work on assigning the platforms to the crates.
Super_Hippo Super_Hippo

2013/12/18

#
@virginia001: Is there any reason why you add your objects to the world and then set their location again?
danpost danpost

2013/12/18

#
@Super_Hippo, when using the 'Save the world' feature within the Greenfoot application, when you first manually add an object to the world, it codes two lines (one creating the object and the other adding it to the world); and if you then drag that object, it adds another line to set its location. Each time you drag it and 'Save the world' again, another 'setLocation' statement is created. Basically, when manually creating a world, every action you perform is stored and when you 'Save the world', every single action that was recorded is written into your 'prepare' method.
virginia001 virginia001

2013/12/18

#
I did this but it made the platform on the opposite side much bigger and the boat is now stuck on that platform the code is now is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Vracht extends World
{

    public Vracht()
    {    
        super(760,760, 1); 

        prepare();
    }

    private void prepare()
    {

        Platform platform = new Platform();  
        addObject(platform, 391, 729);  
        Platform platform2 = new Platform();  
        addObject(platform2, 391, 657);  
        Platform platform3 = new Platform();  
        addObject(platform3, 391, 585);

        vrachtwagenplek vrachtwagenplek = new vrachtwagenplek();
        addObject(vrachtwagenplek, 275, 60);
        vrachtwagenplek vrachtwagenplek2 = new vrachtwagenplek();
        addObject(vrachtwagenplek2, 402, 67);
        vrachtwagenplek vrachtwagenplek3 = new vrachtwagenplek();
        addObject(vrachtwagenplek3, 540, 75);
        vrachtwagenplek vrachtwagenplek4 = new vrachtwagenplek();
        addObject(vrachtwagenplek4, 282, 116);
        vrachtwagenplek vrachtwagenplek5 = new vrachtwagenplek();
        addObject(vrachtwagenplek5, 427, 129);
        vrachtwagenplek vrachtwagenplek6 = new vrachtwagenplek();
        addObject(vrachtwagenplek6, 571, 136);

        vrachtwagenplek.setLocation(275, 53);
        vrachtwagenplek2.setLocation(382, 53);
        vrachtwagenplek3.setLocation(489, 53);
        vrachtwagenplek4.setLocation(274, 125);
        vrachtwagenplek5.setLocation(381, 125);
        vrachtwagenplek6.setLocation(488, 125);

        VrachtwagenZwart vrachtwagenzwart = new VrachtwagenZwart();
        addObject(vrachtwagenzwart, 269, 122);
        VrachtwagenGroen vrachtwagengroen = new VrachtwagenGroen();
        addObject(vrachtwagengroen, 342, 125);
        VrachtwagenOranje vrachtwagenoranje = new VrachtwagenOranje();
        addObject(vrachtwagenoranje, 415, 123);
        VrachtwagenRood vrachtwagenrood = new VrachtwagenRood();
        addObject(vrachtwagenrood, 513, 127);
        VrachtwagenGeel vrachtwagengeel = new VrachtwagenGeel();
        addObject(vrachtwagengeel, 467, 119);

        vrachtwagengroen.setLocation(319, 121);
        vrachtwagenoranje.setLocation(388, 121);
        vrachtwagengeel.setLocation(454, 120);
        vrachtwagenrood.setLocation(514, 119);
        vrachtwagenzwart.setLocation(256, 122);
        groen groen = new groen();
        addObject(groen, 296, 596);
        groen groen2 = new groen();
        addObject(groen2, 502, 734);
        groen.setLocation(287, 590);
        groen2.setLocation(496, 727);
        oranje oranje = new oranje();
        addObject(oranje, 353, 596);
        oranje.setLocation(351, 590);
        oranje oranje2 = new oranje();
        addObject(oranje2, 291, 729);
        oranje2.setLocation(288, 726);
        paars paars = new paars();
        addObject(paars, 411, 667);
        paars.setLocation(412, 589);
        rood rood = new rood();
        addObject(rood, 502, 594);
        rood rood2 = new rood();
        addObject(rood2, 294, 664);
        rood2.setLocation(288, 661);
        paars.setLocation(426, 589);
        oranje.setLocation(356, 590);
        oranje.setLocation(358, 589);
        rood.setLocation(495, 589);
        zwart zwart = new zwart();
        addObject(zwart, 366, 665);
        zwart.setLocation(360, 660);
        zwart zwart2 = new zwart();
        addObject(zwart2, 436, 734);
        zwart2.setLocation(427, 728);
        geel geel = new geel();
        addObject(geel, 496, 660);
        geel geel2 = new geel();
        addObject(geel2, 361, 728);
        geel2.setLocation(359, 727);
        zwart.setLocation(359, 661);
        oranje2.setLocation(427, 663);
        paars paars2 = new paars();
        addObject(paars2, 294, 729);
        paars2.setLocation(288, 727);
        vrachtwagenzwart.setLocation(248, 122);
        vrachtwagengroen.setLocation(297, 121);
        vrachtwagenoranje.setLocation(358, 121);
        vrachtwagengeel.setLocation(421, 120);
        vrachtwagenrood.setLocation(476, 120);
        VrachtwagenPaars vrachtwagenpaars = new VrachtwagenPaars();
        addObject(vrachtwagenpaars, 525, 125);
        vrachtwagenpaars.setLocation(522, 120);
        vrachtwagenoranje.setLocation(350, 121);
        vrachtwagengeel.setLocation(407, 121);
        vrachtwagenrood.setLocation(468, 120);
        vrachtwagenrood.setLocation(467, 120);
        vrachtwagenrood.setLocation(461, 120);
        vrachtwagenpaars.setLocation(515, 120);

        Schipgroen schipgroen = new Schipgroen();
        addObject(schipgroen, 637, 96);
    }
}
danpost danpost

2013/12/18

#
What is the size of the image that you set as default for your Platform class?
virginia001 virginia001

2013/12/18

#
I don't know but i suggest we will proceed tomorrow i need to get some sleep
There are more replies on the next page.
1
2
3
4
5
6
7