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

2014/12/8

Selecting/Deselecting

Stoeptegel Stoeptegel

2014/12/8

#
Hello everyone! I have been busy with a game where you can control different ships on water. I can select the ships with the left mouse button, and move them with the right mouse button. By left clicking on an already selected ship deselects the ship. But i want to make it in a way that when you select another ship, the other ship gets deselected automatically. I was thinking about a variable which can hold one ship (the selected ship), but i can't figure it out (still a beginner).
boolean state = false;
GreenfootImage myImage;
public void act() 
{
    selectBoat();
    if (state == true) {
        clickMove();
    }
}
And here the methods involved.
public void selectBoat() {
        if (Greenfoot.mouseClicked(this)) {
            if(Greenfoot.getMouseInfo().getButton() == 1) {
                if (state == false) {
                    state = true;
                    myImage = new GreenfootImage("boatTest_t.png");
                    setImage(myImage);
                } else if (state == true) {
                    state = false;
                    myImage = new GreenfootImage("boat01-f.png");
                    setImage(myImage);
                }    
            }
        }

public void clickMove () {
        if (Greenfoot.getMouseInfo() != null) {
            if (Greenfoot.getMouseInfo().getButton() == 3) {
                int mouseX = Greenfoot.getMouseInfo().getX();
                int mouseY = Greenfoot.getMouseInfo().getY();
                this.turnTowards(mouseX, mouseY);
            }    
        }
    }
I appreciate any help. Thank you for checking this out and have a nice day. Stoeptegel
danpost danpost

2014/12/8

#
Stoeptegel wrote...
I was thinking about a variable which can hold one ship (the selected ship), but i can't figure it out (still a beginner).
This approach will work. This change, however, will require the field be in a location that encompasses all boats -- not just one. Since all boats are in the world, probably the World subclass would be a good location. The Boat class will not need a 'state' field (btw, 'state' is a terrible name for a field; the name tells you nothing about which state the field is for; 'active' might be better, or 'activeState', and 'isActive' would even be better). You can add 'get' and 'set' methods to the World subclass for you boats to deal with the field:
// instance field in world subclass
private Actor activeBoat = null;
// method to set field value
public void setActiveBoat(Actor boat)
{
    activeBoat = boat;
}
// method to get field value
public Actor getActiveBoat()
{
    return activeBoat;
}
From the Boat class, (and with me temporarily renaming your subclass of World to 'MyWorld'), you can use something like the following:
if (Greenfoot.mouseClicked(this)) ((MyWorld)getWorld()).setActiveBoat(this);
and for moving:
if (((MyWorld)getWorld()).getActiveBoat() == this && mouseClicked(getWorld())
Stoeptegel Stoeptegel

2014/12/9

#
Thanks alot danpost! It's way less code and it just makes total sense,. :) Instead of the mouseClicked i did use Greenfoot.getMouseInfo().getButton() == x Because i wanted to use the left and right mouse button for different purposes. I assume that isn't bad? Have a nice day Stoeptegel
Stoeptegel Stoeptegel

2015/1/15

#
I found the solution, thanks for all your help!
You need to login to post a reply.