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

2023/11/28

help!!!!

MARTINabab MARTINabab

2023/11/28

#
How can I make a cycle where a character actor walks over a ground actor and when that ground actor disappears from the screen it is generated again after another ground actor?
danpost danpost

2023/11/29

#
MARTINabab wrote...
How can I make a cycle where a character actor walks over a ground actor and when that ground actor disappears from the screen it is generated again after another ground actor?
Have width of ground actor equal to width of world. Then, create two Ground actors and place them one world width apart from each other. You will need an unbounded world for this to work, so the two ground actors can play "leap-frog" with each other:
import greenfoot.*;

public class Ground extends Actor
{
    public void act() {
        int worldWidth = getWorld().getWidth();
        if (getX() < worldWidth/2) {
            move(worldWidth*2);
        }
        if (getX() >= worldWidth*3/2) {
            move(-worldWidth*2);
        }
    }
}
You need to login to post a reply.