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

4
5
6
7
8
9
10
virginia001 virginia001

2013/12/20

#
so all the the movement methods in Schip need to have this code but the (getX() - speed, getY()) need to be adjusted different depending on the direction it goes?
virginia001 virginia001

2013/12/20

#
I have changed the code it looks like this....
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;  
    }
}
danpost danpost

2013/12/20

#
That looks pretty good. I pray crates can be loaded now.
virginia001 virginia001

2013/12/20

#
nopeee...thought so too but no result.
danpost danpost

2013/12/20

#
First, when I saw that all 12 platforms made one huge platform and not 3 somewhat larger platforms, I realized that we will have to adjust the Platform constructor to create a larger image and only add one Platform object into the world. Change the Platform class constructor to this:
public Platform()
{
    GreenfootImage pImg = getImage();
    GreenfootImage image = new GreenfootImage(251, 251);
    for (int i=0; i<4; i++) for (int j=0; j<3; j++)
    {
        image.drawImage(pImg, 72*i, 72*j);
    }
    setImage(image);
}
In your world 'prepare' method, remove and replace this:
Platform platform = new Platform();
addObject(platform, 391, 729);
Platform platform2 = new Platform();
addObject(platform2, 391, 657);
Platform platform3 = new Platform()
addObject(platform3, 391, 585);
with this:
Platform platform = new Platform();  
addObject(platform, 391, 674);
Then, we can set the 'platform' fields of the Kratten objects. After compiling, right click on each barrel, select 'void setPlatform(Platform p)', click on the platform and click the OK button. Then save the world so they will set automatically when compiling again.
virginia001 virginia001

2013/12/20

#
I have now 6 objects because your previous code made my platform to small so i added 3 more. i tried your code but it messed up my whole platforms again
virginia001 virginia001

2013/12/20

#
i think i need to give you an better image of my scenario because maybe your a little confused
danpost danpost

2013/12/20

#
Show all code in your 'prepare' method dealing with Platform and vrachtwagenplek objects (in the order they appear in the method).
virginia001 virginia001

2013/12/20

#
private void prepare()
    {

        Platform platform = new Platform();  
        addObject(platform, 408, 729);  
        Platform platform2 = new Platform();  
        addObject(platform2, 550, 657);  
        Platform platform3 = new Platform();  
        addObject(platform3, 730, 585);
        Platform platform4 = new Platform();  
        addObject(platform4, 408, 729);  
        Platform platform5 = new Platform();  
        addObject(platform5, 550, 657);  
        Platform platform6 = new Platform();  
        addObject(platform6, 408, 585);

platform4.setLocation(449, 756);
        platform.setLocation(333, 756);
        platform5.setLocation(333, 684);
       
        platform2.setLocation(449, 684);
        platform3.setLocation(449, 612);
        platform6.setLocation(333, 612);

        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);
        vrachtwagenplek5.setLocation(381, 125);
        vrachtwagenplek6.setLocation(489, 125);
        vrachtwagenplek4.setLocation(275, 125);

danpost danpost

2013/12/20

#
Ok, lets try this again. Change your Platform class constructor to this:
public Platform()
{
    GreenfootImage base = getImage();
    GreenfootImage image = new GreenfootImage(252, 216);
    for (int i=0; i<252; i+=72) for (int j=0; j<216; j+=72)
    {
        image.drawImage(base, 72*i, 72*j);
    }
    setImage(image);
}
Then, change your vrachtwagenplek class constructor to this:
public vrachtwagenplek()
{
    GreenfootImage base = getImage();
    GreenfootImage image = new GreenfootImage(468, 252);
    for (int i=0; i<468; i+=72) for (int j=0; j<252; j+=72)
    {
        image.drawImage(base, 72*i, 72*j);
    }
    setImage(image);
}
Finally, remove all the code you just posted from within your 'prepare' method above (starting at line 4) and replace it with something like this:
Platform platform = new Platform();  
addObject(platform, 380, 652);  
vrachtwagenplek vrachtwagenplek = new vrachtwagenplek();
addObject(vrachtwagenplek, 380, 126);
You can then, adjust the position of the two platforms, set the 'platform' field of the Kratten objects and 'Save the world'.
Super_Hippo Super_Hippo

2013/12/20

#
I think you should delete both 72 out of the lines 07.
virginia001 virginia001

2013/12/20

#
I have adjusted the code how you said but now i have one small pixel for crate platform and one small pixel for my truck platform, so i need to change the size of the images.....how can i do that
danpost danpost

2013/12/20

#
Super_Hippo wrote...
I think you should delete both 72 out of the lines 07.
You're right. I change the 'for' parameters, but did not compensate within the block in the 'drawImage' call. Remove all the '72*' occurrences from the 'drawImage' lines in both constructors.
virginia001 virginia001

2013/12/22

#
Heyy back againn....My platforms have the perfect size now and they're both one object so i think we can start with getting the crate objects now...
danpost danpost

2013/12/22

#
Alright, compile your project and then for each of your crates, right-click on the crate, select 'setPlatform(Platform p)' and then click on the crate platform itself (this puts a reference to the platform in the input box) following by clicking on the 'OK' button. After that is done for all the crates, 'Save the world'.
There are more replies on the next page.
4
5
6
7
8
9
10