How to get the name of an object from another class in Java

I am using Greenfoot IDE and I have World Class and Boat class and Exit class

In the "Boat" class, I have a constructor that defines the boat (which type of boat and which photograph).

Boat code:

public class Boat extends Actor
{
    private int size;
    private int speed;
    private int slow = 1;
    private boolean leeg = false;
    private int id;
    private int time;

    public void act() {
         MoveBoat();
         MoveMouse();
         ExitHarbor(time);
         Dock();
         Colission();


    }
    public Boat(int newSize, int i, int t) {
        size = newSize;
        id = i;
        time = t;
        setImage(id);
    }

    public void setImage(int i) {
        if (!leeg) {
            setImage(new GreenfootImage("Boat"+i+".png"));
        }
        else {
            setImage(new GreenfootImage("Boatleeg"+i+".png"));
        }
    }
}

      

Inside the Exit class, I have a constructor that defines another Exit

Output:

public class Exit extends Actor
{
    private String color;
    private int points;

    /**
     * Act - do whatever the Exit wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Exit(String kleur) {
        color = kleur;
        setImage(kleur);
    }

    public void setImage(String kleur) {
        //Game1 game = getWorld().getObjects(Boat.class);
        setImage(new GreenfootImage("exit"+kleur+".png"));
    }
    public String getColor()
{
    return color;
}
}

      

In the World class I added 3 different boats and 3 different exits to the so-called "world" of green color with different parameters. Now I have 3 different boats and 3 different exits.

World:

public class Game1 extends World
{

    /**
     * Constructor for objects of class Game1.
     * 
     */
    public Game1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 900, 1); 
        prepare();
    }

    public void prepare()
    {
        Exit exit1 = new Exit("paars");
        addObject(exit1, 885, 615);
        Exit exit2 = new Exit("groen");
        addObject(exit2, 885, 472);
        Exit exit3 = new Exit("geel");
        addObject(exit3, 885, 340);
        Boat boat1 = new Boat(10,1,700);
        addObject(boat1, 500,61);
        Boat boat2 = new Boat(20,2,500);
        addObject(boat2, 800,61);
        Boat boat3 = new Boat(30,3,300);
        addObject(boat3, 650,61);
}
}

      

The problem I am facing at the moment is that I will not indicate 1 boat exit until 1. To clear it anymore, I don’t want boat 1 to be able to only interact with "Geel" Exit (or exit1) ...

I've already tried some code, but I can't seem to get it to work.

Trial code:

if (id == 1 && "geel".equals(exit.getColor()))

      

I think I can get it to work with this, if only I need to get objects from the World class or the Boat class to do this, I don't know how? I tried

Actor boat = (Actor)getWorld().getObjects(Boat.class).get(0);

      

But this doesn't return 3 different Boats (including their object (variable) names)

Are there any suggestions?

ps This is more code, but I only showed the code that is needed for this problem

after Reactions I've tried but I'm stuck again.

    public void ExitBoat(Exit exit, int size) {
    this.exit = exit;
    System.out.println(exit);
   /* if(exit == exit1 && size == 10) {
        System.out.println("jdjdf");
    }*/
}
    public Boat(Exit uitgang, int newSize, int i, int t) {
    exit = uitgang;
    size = newSize;
    id = i;
    time = t;
    setImage(id);
}

      

now i'm stuck again, i dont know how to call the method with correct parameters in my void public action.

+3


source to share


1 answer


You can add Exit as a field in Boat class

private Exit exit;

      



add an exit to the boat constructor so you can create Boat instances like this.

Boat boat1 = new Boat(exit1, 10,1,700);

      

+1


source







All Articles