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

2024/10/1

Actor remembering what world they are in?

trulydevious trulydevious

2024/10/1

#
I thought I did the coding right, but I guess not. It's for an assignment in my class and the code I'm trying to figure out just won't work. I copied what my teacher gave me so that code is right, but I don't know if I'm creating the field correctly. This is my world class:
//FrogWorld
public class FrogWorld extends World
{
    private boolean upVehicleExists;
    private boolean downVehicleExists;
    public static FrogWorld world;
    /**
     * Constructor for objects of class FrogWorld.
     * 
     */
    public FrogWorld()
    {    
        // Create a new world with 1200x400 cells with a cell size of 1x1 pixels.
        super(1200, 400, 1);
        downVehicleExists = false;
        upVehicleExists = false;
        setup();
        Water.initializeImages();
        Vehicle.initializeImages(100);
    }
    
    public static FrogWorld getWorldInstance()
    {
        return world;
    }
This is the code I need help with:
    public void addedToWorld(World world){
        this.world = (FrogWorld) world;
    }
The error says "world is not public in greenfoot.Actor; cannot be accessed from outside package" These were my teacher's instructions on this bit: To make things a little easier, we'll have the Vehicles remember what world they are in. If you'll recall, Greenfoot gives us three opportunities for code to execute: When an object is created, any code that is in or called from its constructor is executed. This happens once when the object is created. When an object's act method is called, any code that is in or called from its act method is executed. This happens repeatedly. When the program is running, Greenfoot loops through all of the Worlds and Actors and calls their act methods, provided they have one. When an object is added to a world, Greenfoot calls the object's addedToWorld method. Classes do not have a definition for this method by default, so you have to add it if you want it to happen. We're going to write an addedToWorld method for Vehicle that simply remembers the world it was added to. (the code in Vehicle here) I've tried looking it up but I can't seem to find anything that makes sense to me, and my teacher is gone this week. Can someone help?
danpost danpost

4 days ago

#
There is no need for line 4 at all. The getWorld method in the Actor class will return any world the frog is in. If the frog needs to determine which type World object it is in, if any, you can use something like the following structure:
if (getWorld() instanceof FrogWorld) {
    // whatever the frog does in FrogWorld
}
else if (getWorld() instanceof MyWorld) {
    // whatever the frog does in MyWorld
}
danpost danpost

4 days ago

#
As far as your code is concerned, please provide the full class code in which the addedToWorld method is in, starting at line 1.
You need to login to post a reply.