This site requires JavaScript, please enable it in your browser!
Greenfoot back
13111876
13111876 wrote ...

2013/10/31

Help with objects follow each other.

13111876 13111876

2013/10/31

#
Hey all :), Can someone help me with my problem. I have got my intersecting objects following my boat so far, but I also want my boat to sink en when that happens the objects that are in the boat (intersecting) have to sink with it. The last part is my problem. Can some one help me with this. My code:
public boolean isOverWeight()
    {
        List<Persoon> personen = (List<Persoon>)getIntersectingObjects(Persoon.class);   
        int currentWeight = 0;
        for (Persoon persoon : personen)
        {
            currentWeight += persoon.getWeight() ;

        }
        
        if (currentWeight >= 100)
        {
            setLocation (getX(), getY() +1);
            //setLocation (List<Persoon>)getIntersectingObjects(Persoon.class) (getX() getY() +5);
        }
        
        else if (currentWeight >= 200)
        {
            setLocation (getX(), getY() +1);
            //setLocation (List<Persoon>)getIntersectingObjects(Persoon.class) (getX() getY() +5);
        } 
        return currentWeight > 300;
        
    }
Thanks.
davmac davmac

2013/10/31

#
I guess that by "sink" you mean move down the screen, i.e. lines 13 and 19. You can make the intersecting objects do that by iterating through them with a loop:
for (Persoon persoon : personen) {
    persoon.setLocation(persoon.getX(), persoon.getY() + 1);
}
13111876 13111876

2013/10/31

#
@davmac first of all thanks for the reply it helped me a lot. But now I want to have also the list of dier(animal) in the same code. I did something like this.
public boolean isOverWeight()
    {
        List<Persoon> personen = (List<Persoon>)getIntersectingObjects(Persoon.class);   
        int currentWeight = 0;
        for (Persoon persoon : personen)
        {
            currentWeight += persoon.getWeight();
        }
        
        if (currentWeight >= 200)
        {
            for (Persoon persoon : personen) 
            {  
                setLocation (getX(), getY() +1);
                persoon.setLocation(persoon.getX(), persoon.getY() + 4);
            }
            //setLocation (List<Persoon>)getIntersectingObjects(Persoon.class) (getX() getY() +5);
        }
        
        List<Dier> dieren = (List<Dier>)getIntersectingObjects(Dier.class);   
        //int currentWeight = 0;
        for (Dier dier : dieren)
        {
            currentWeight += dier.getWeight();
        }
        
        if (currentWeight >= 200)
        {
            for (Dier dier : dieren) 
            {  
                setLocation (getX(), getY() +1);
                dier.setLocation(dier.getX(), dier.getY() + 4);
            }
            //setLocation (List<Persoon>)getIntersectingObjects(Persoon.class) (getX() getY() +5);
        }
        return currentWeight > 500;
    }
But what happens is that the boot doesn't sink.
You need to login to post a reply.