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

2019/11/18

Spawning an actor on the right side of the world

MLGwarfare MLGwarfare

2019/11/18

#
I need help making an actor spawn on the right side of my world at any y value, making them move to the left, and despawning on the left. I have code for the oil to move to the left at a constant rate and despawn when it hits the world border:
public class oil extends Actor
{
    public void act()
    {
        move(4);
        if (isAtEdge()) getWorld().removeObject(this);
    }  
}
I don't know if that code is usable in any way for spawning actors on the right side of the map, but It might help with making a solution.
MLGwarfare MLGwarfare

2019/11/18

#
Also I am fairly new to Greenfoot and plug in code that I can find online, so don't expect me to know a ton of things.
danpost danpost

2019/11/18

#
MLGwarfare wrote...
I don't know if that code is usable in any way for spawning actors on the right side of the map, but It might help with making a solution.
Your World subclass codes would be more useful as that is where you would spawn the oil objects.
MLGwarfare MLGwarfare

2019/11/18

#
I could use either one because I really don't know how to code, i just know where to put it where someone tells me to
danpost danpost

2019/11/18

#
MLGwarfare wrote...
I could use either one because I really don't know how to code, i just know where to put it where someone tells me to
Okay. Add an act method into your class that extends World and, in it, call a new method that will do what you want. In the new method, on random chance, spawn an oil object at a random y value with an x value set at the width of the world.
danpost danpost

2019/11/18

#
MLGwarfare wrote...
I have code for the oil to move to the left at a constant rate
If you want your actor to move left (without rotating the actor 180 degrees first), then line 5 in your oil class code above should be:
move(-4);
MLGwarfare MLGwarfare

2019/11/19

#
Ok thank you I got that to work, but I need one more thing to finish my game. I have been trying to add a game over screen when the car you control gets hit by an oil actor. i have been trying this code:
public class tile extends World
{   
    private int oilSpawn=100, oilSpawnTimer=oilSpawn;
    Actor car;
    public tile()
    {
        super(1920, 1080, 1);
        prepare();
    }

    public void started()
    {
        Greenfoot.playSound("earrapeflamingo1.mp3");
    }

    public void act()
    {
        if (--oilSpawnTimer==0)
        {
            oilSpawnTimer=oilSpawn;
            addObject(new oil(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        }
        if (car != null)
        {
            World world = getWorld(); //error line
            world.removeObjects(world.getObjects(null));
            world.setBackground("karenoverjpg");
            Greenfoot.stop();
        }
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        karen karen = new karen();
        addObject(karen,964,86);
        karen.setLocation(986,103);
        kid kid = new kid();
        addObject(kid,1015,112);
        car car = new car();
        addObject(car,944,684);
        for(int i = 0; i < 15; i++) {
            int x = Greenfoot.getRandomNumber(getWidth() - 1);
            int y = Greenfoot.getRandomNumber(getHeight() - 1);
            addObject(new oil(), x, y);
        }
    }

}
I keep getting an error for the getWorld (showed in code) and I do not know how to make it work. do you know how I would be able to change the code to make it compile correctly?
MLGwarfare MLGwarfare

2019/11/19

#
Forgot to say I have that code in my world class
danpost danpost

2019/11/19

#
MLGwarfare wrote...
I keep getting an error for the getWorld (showed in code) and I do not know how to make it work. do you know how I would be able to change the code to make it compile correctly?
The getWorld method is an Actor class method. You cannot execute it on a World object. Anyway, the code from line 23 to line 28 appears to be something an oil object might want to do -- not the World object.
You need to login to post a reply.