Search code examples
javaclass2d-gamesgreenfoot

How to get name of the object's from a different class in Java


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

Inside the Boat class i have a constructor which defines the boat (what kinda boat it is & which picture).

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 which defines the different Exit's

Exit:

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 & 3 different exits to the so called "World" of greenfoot with different parameters. Now i have 3 different boats & 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'm having at the moment is that i wan't to specify 1 boat to 1 exit. To clear it more i wan't that boat 1 can only interact with the "geel" Exit (or exit1).

I already tried some code but i can't get it to work.

Tried code:

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

I think i can make it work with this if but for that i need to retrieve the objects from the World class or from the Boat class, I don't know how to do it? i tried

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

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

Anyone any suggestions?

p.s It is more code but i only showed the code which is necesarry for this problem

after The reactions i tried some 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 the right parameters in my public void act.


Solution

  • You could add Exit as a field in the Boat class

    private Exit exit;
    

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

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