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

2014/12/16

Different exit's interacting with different objects

berdie berdie

2014/12/16

#
I don't know if u have any tips but i have 3 different exit's
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) {
        setImage(new GreenfootImage("exit"+kleur+".png"));
    }
World:
        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:
public class Boat extends Actor
{
    private int size;
    private int speed;
    private int slow = 1;
    private boolean leeg = false;
    private boolean boatEmpty = false;
    private int spawnCounter = 0;  
    private int id;

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

    public void setImage(int i) {
        if (!leeg) {
            setImage(new GreenfootImage("Boat"+i+".png"));
        }
        else {
            setImage(new GreenfootImage("Boatleeg"+i+".png"));
        }
    }
World:
        Boat boat1 = new Boat(10,1);
        addObject(boat1, 800,61);
        Boat boat2 = new Boat(20,2);
        addObject(boat2, 800,61);
        Boat boat3 = new Boat(30,3);
        addObject(boat3, 800,61);
Now i also have 3 different boats (nearly created the same way). But i want to know how can i have the the different sized boats interact with the 3 different colors of the exits (so like Boat 1 will only interact with Exit("geel") or something like that. pls help:S
danpost danpost

2014/12/16

#
You need a getter method in the Exit class for the value of the color string; so the boats can make the following conditional checks:
if (id == 1 && "geel".equals(exit.getColor()))
(or something like that) for each 'id' value.
berdie berdie

2014/12/16

#
Thanks that is what i was looking for! But how do i call the boat inside the exit class? just by making a var Boat boat?
danpost danpost

2014/12/16

#
What I gave above was for when the boat detected an Exit object. But, you could do it the other way around. Instead of a 'getColor' method in the Exit class, have a 'getID' method in the Boat class. Then have the Exit objects look for intersecting boats and get the id of any that do intersect and then make pretty much the same comparisons.
berdie berdie

2014/12/17

#
@danpost I've made a
    private Exit exit;
In the class Boat, and i also recreated my constructor for boat
    public Boat(int newSize, int i, int t, Exit out) {
        size = newSize;
        id = i;
        time = t;
        exit = out;
        setImage(id);
    }
in world i added:
        Boat boat1 = new Boat(10,1,700,exit1);
        addObject(boat1, 500,61);
        Boat boat2 = new Boat(20,2,500,exit2);
        addObject(boat2, 800,61);
        Boat boat3 = new Boat(30,3,300,exit3);
        addObject(boat3, 650,61);
exit1 as parameter of boat. now i trun to run this:
    public void act() {
         MoveBoat();
         MoveMouse();
         ExitHarbor(time);
         Exit(id,exit);
         Dock();
         Colission();
         
    }
    public void Exit(int j, Exit out) {
       if (j == 1 && out == exit1 ){
            System.out.println( "dfdf");
        }
    }
but that causes an Error cannot find symbol variable (exit1)
danpost danpost

2014/12/17

#
After all your changes, you only need to ask 'if (intersects(out))'. Should work regardless of which boat is asking.
berdie berdie

2014/12/17

#
Ty very much was struggling whole day too make this work!
You need to login to post a reply.