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

2013/10/16

Help with objects overlaying each other

13111876 13111876

2013/10/16

#
Hello, How can I prevent that my objects keep overlaying each other. This is my code till now: public void followBoot() { if(getOneIntersectingObject(Boot.class)!= null) { if (Greenfoot.isKeyDown("left") && getX() > 303) { move(-5); } if (Greenfoot.isKeyDown("right") && getX() < 772) { move(5); } } } So what they do right now is they move when I press left or right, then they go till a certain X Y coordinate but when they get there the overlay each other. Can someone help me get this right. I want the objects to stay correct and don't overlay each other.
Gevater_Tod4711 Gevater_Tod4711

2013/10/16

#
I think the problem could be fixed by checking whether they overlay each other after moving. Try to add another if clause after the move method calls like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void followBoot() {
if(getOneIntersectingObject(Boot.class)!= null) {
    if (Greenfoot.isKeyDown("left") && getX() > 303) {
        move(-5);
        if (getOneIntersectingObject(Boot.class)!= null) {
            move(5);//move back;
        }
    }
    if (Greenfoot.isKeyDown("right") && getX() < 772) {
        move(5);
        if (getOneIntersectingObject(Boot.class)!= null) {
            move(-5);//move back;
        }
    }
 }
}
danpost danpost

2013/10/16

#
If you want an object to follow another, do not try to move the object separately with key-down coding. Either have the Boot object control the location of this object, or have this object use the location of the Boot object to determine where it should be.
1
2
3
4
5
Actor boot = getOneIntersectingObect(Boot.class);
if (boot != null)
{
    setLocation(boot.getX()+xOffset, boot.getY()+yOffset);
}
'xOffset' and 'yOffset' are the differences in the coordinates between the Boot and the follower.
13111876 13111876

2013/10/16

#
@danpost Where do I set this code?? @Gevater_Tod4711 This didn't work, there was no syntax error but the code changed and now the 'Boot' leaves with out the other objects.
danpost danpost

2013/10/16

#
Set the code either in the 'followBoot' method (called by the 'act' method) or directly in the 'act' method.
13111876 13111876

2013/10/16

#
@danpost Thank you first of all, I think I wasn't clear enough. The problem is that the multiple objects that I have still overlay each other. I got this boat = 'Boot' and I put objects in this boat, I move it with the left and right keys till a certain X Y coordinate. The objects follow good, the problem is when the boat is at the certain X Y coordinate the objects overlay each other. The images overlay each other.
danpost danpost

2013/10/16

#
Maybe these objects (that you put in the boat) should keep track of their offsets with respect to the location of the boat.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// with instance fields
Boot boat = null;
int xOffset, yOffset;
// placing into boat
public void placedInBoat(Boot b)
{
    boat b;
    xOffset = getX()-boat.getX();
    yOffset = getY()-boat.getY();
}
// moving
public void move()
{
    if (boat != null) setLocation(boat.getX()+xOffset, boat.getY()+yOffset);
}
// removing from boat
public void removedFromBoat()
{
    boat = null;
]
Gevater_Tod4711 Gevater_Tod4711

2013/10/16

#
I'm not quite shure if this will work but you could try to use this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void followBoot() { 
if(!getObjectsInRange(getImage().getWidth() + 25, Boot.class).isEmpty()) { 
    if (Greenfoot.isKeyDown("left") && getX() > 303) { 
        move(-5); 
        while (getOneIntersectingObject(Boot.class) != null) { 
            move(1);//move back one step; 
        
    }  
    if (Greenfoot.isKeyDown("right") && getX() < 772) {  
        move(5); 
        if (getOneIntersectingObject(Boot.class) != null) { 
            move(-1);//move back one step; 
        
    }  
 
}
danpost danpost

2013/10/16

#
Line 7 of my last code post should read:
1
boat = b;
You need to login to post a reply.