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

2024/10/2

#
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

2024/10/2

#
As far as your code is concerned, please provide the full class code in which the addedToWorld method is in, starting at line 1.
trulydevious trulydevious

2024/10/17

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Vehicle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Vehicle extends Actor
{
    private static GreenfootImage[] images = new GreenfootImage[5];
    private int speed;

    public Vehicle(boolean direction){
        speed = Greenfoot.getRandomNumber(3) + 1;
        if(!direction){
            speed = -speed;
        }

        if(speed < 0){
            setRotation(180);
        }
    }

    /**
     * Act - do whatever the Vehicle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setRandomImage();
        moveVehicle();
    }

    public static void initializeImages(int width){
        for(int i = 0; i < 5; i++)
        {
            images[i] = new GreenfootImage("car"+i+".png");
            int w = images[i].getWidth();
            int h = images[i].getHeight();
            int height = (int)((double)width/w*h);
            images[i].scale(width, height);
        }
    }

    private void setRandomImage(){
        if(images[0] != null)
        {
            int randomIndex = Greenfoot.getRandomNumber(5);
            setImage(images[randomIndex]);
        }
    }

    private void moveVehicle(){
        setLocation(getX(), getY() + speed);
        if(getY() <= 0 || getY() >= getWorld().getHeight()){
            getWorld().removeObject(this);
        }
    }

    public void addedToWorld(World world){
        this.world = (FrogWorld) world;
    }
}
danpost danpost

2024/10/17

#
Since it was part of the assignment, you will need to include the field: (see line 6):
import greenfoot.*;

public class Vehicle extends Actor {
    private static GreenfootImage[];
    
    private FrogWorld frogWorld;
    private int speed;
    
    public Vehicle(boolean direction){
        speed = Greenfoot.getRandomNumber(3) + 1;
        if (!direction){
            speed = -speed;
            setRotation(180);
        }
        setRandomImage();
    }
    
    public void act() 
    {
        moveVehicle();
    }
 
    public static void initializeImages(int width){
        images = new GreenfootImage[5];
        for (int i=0; i<5; i++)
        {
            images[i] = new GreenfootImage("car"+i+".png");
            int w = images[i].getWidth();
            int h = images[i].getHeight();
            int height = width*h/w;
            images[i].scale(width, height);
        }
    }
 
    private void setRandomImage(){
        if(images != null)
        {
            int randomIndex = Greenfoot.getRandomNumber(images.length);
            setImage(images[randomIndex]);
        }
    }
 
    private void moveVehicle(){
        setLocation(getX(), getY() + speed);
        if (isAtEdge()){
            getWorld().removeObject(this);
        }
    }

    protected void addedToWorld(World world) {
            frogWorld = (FrogWorld) world;
    }
}
In your FrogWorld class codes, remove the "public static FrogWorld getWorldInstance()" method. There is no variable named "world" that is accessible there.
You need to login to post a reply.