Hints & Answers

How do I spawn obstacles in?

Greenfoot provides the method getRandomNumber(int) which we can use to spawn the obstacles randomly.

If we place the following code into the act() method of the World, then we can add Rocks to the world at random intervals.

if (Greenfoot.getRandomNumber(200) < 4) {
    Rock rock = new Rock();
    addObject(rock, 899, 490);
}

Why aren't my Rocks moving?

Try it out, you might notice that the Rocks are spawned but they don't move at all, they just stay at the bottom right of the map.

Greenfoot provides a method move(int) which can be used to move Actors

move(-3);

Why aren't my Rocks disappearing when they reach the end?

In Greenfoot, Actors are limited to the World, they can't be moved off the screen. Therefore we must manually remove them when they reach the other edge of the World.

Also in the act method, we can add the following logic that will remove the object when the coordinates are close to the edge of the map.

if (getX() <= 10) {
    getWorld().removeObject(this);
}

results matching ""

    No results matching ""