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

2024/1/24

My lava block is moving over the edge of the platform!

I3litz I3litz

2024/1/24

#
public void bewegung()
    {
        move(x);
        setLocation(getX(), getY()+2);
        Actor platform = 
        getOneIntersectingObject(Plattform.class);
        setLocation(getX(), getY()-2);
        int pX = platform.getX();
        int pW = platform.getImage().getWidth();
        if ((x < 0 && pX-pW/2 >= getX()) || (x > 0 && pX+pW/2 <= 
        getX())) 
        {
            x = -x;
        }
    }
 
The lava block is going over the edge of the platform, it doesn't look nice and the game is harder to complete. I need help.
danpost danpost

2024/1/25

#
I3litz wrote...
<< Code Omitted >> The lava block is going over the edge of the platform, it doesn't look nice and the game is harder to complete. I need help.
It would be easier if, instead of looking ahead to see if a collision will occur, to just let it happen and check for that. That way to can immediately rectify the situation -- undo the collision and change moving direction. Something like this:
public void bewegung() {
    move(x);
    if (isTouching(Plattform.class)) {
        x = -x;
        move(x);
    }
}
I3litz I3litz

2024/1/25

#
The lava block is inside the platform, it moves right to left as it should, but as i said it moves over the edge of the platform. I want that it stays inside of the platform. I cant solve it, pls help. And what you showed me it doesnt work or i dont know where to put it in.
danpost danpost

2024/1/26

#
I3litz wrote...
The lava block is inside the platform, it moves right to left as it should, but as i said it moves over the edge of the platform. I want that it stays inside of the platform. I cant solve it, pls help. And what you showed me it doesnt work or i dont know where to put it in.
Oh, I presumed you wanted it outside (not inside) the platform. This will be a lot more involved. Still, it is better to let the event happen and then make the appropriate adjustments (undo the event and change the direction of motion). To accomplish that, the lava objects could have a "feeler" at each side. It would be something like this:
/** in the Lava class */
// added fields
private End leftEnd, rightEnd;

// added method (overriding method)
protected void addedToWorld(World world) {
    leftEnd = new End();
    rightEnd = new End();
    int width = getImage().getWidth();
    world.addObject(leftEnd, getX()-width/2-2, getY());
    world.addObject(rightEnd, getX()+width/2+2, getY());
}

// in move(int) method, add the following two lines
leftEnd.move(x);
rightEnd.move(x);

// the bewegung method
public void bewegung() {
    move(x);
    if (leftEnd.extended() || rightEnd.extended()) {
        x = -x;
        move(x);
    }
}

// added class (inside the Lava class)
protected class End extends Actor {
    public End() {
        setImage(new GreenfootImage(3, 3));
    }
    
    public boolean extended() {
        return ! isTouching(Plattform.class);
    }
}
If the move(int) method is not directly in the Lava class, the add the following to the Lava class:
public void move(int x) {
    super.move(x);
    leftEnd.move(x);
    rightEnd.move(x);
}
I3litz I3litz

2024/1/26

#
Thank you so much your truely the Greenfoot-Master.
You need to login to post a reply.