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

2014/12/6

Select an object to move

kevin2106 kevin2106

2014/12/6

#
I am working on this game where i want to select a boat with my left mouse button and to control it after i selected it. But i cant seem to find anything about this and i have no idea where to start. Is there someone who can help me?
danpost danpost

2014/12/6

#
You will need a single field to hold the boat selected and use it to restrict user-controlled movement of all boats except the selected one.
kevin2106 kevin2106

2014/12/6

#
Okay, could you give me an example, i have tryed this already but it is not really working. im new to greenfoot so its really hard to understand where to start.
    private boolean state = false;
                                   
    public void act() 
    {   
        
        //locationGetting();
        checkMargins();
        checkEdges();
        boatSelector();

    }
    
   public void boatSelector()
    {
        if(Greenfoot.mouseClicked(this)){
            this.state = true;
            if (this.state == true){
                boatMovement();
            }
            else{
                
            }
        }
    }
kevin2106 kevin2106

2014/12/6

#
Okay i found a solution now but my next question is how to deselect a boat after i selected one this is the code i used to select them:
if (state == false)
        {  
            if (Greenfoot.mousePressed(this)) 
            {  
            state = true;  
            } 
            
        } 
        else if (state == true) 
        {  
            boatMovement();  
        }  
danpost danpost

2014/12/6

#
That may be fine if you only had one boat object. If you have multiple boat object, you could end up controlling more than one at a time. An object reference field that actually references the currently active boat would be the way to go with multiple boats.
JustMike JustMike

2014/12/6

#
I'm a beginner as well, but maybe this will help.
private boolean state = false;

public void act()   
 {     
       
     //locationGetting();  
     checkMargins();  
     checkEdges();  
     boatSelector();
     if (state == true)
     {
        boatMovement();
     }
 }  
   
public void boatSelector()  
 {  
     if (Greenfoot.mouseClicked(this)) {  
         if (state == false) //if the object is not yet selected, select the object
         {
             state = true;
         } else {  //if you click on the object if it is selected (state=true) it will set the state to false, thus deselecting the object
             state = false;
         }  
     }  
 }  
This code will make sure you can select the boat and you will only be able to deselect the boat if you click on it again. You might want to make an image that shows the boat that is selected. It is also an easy way to see if it is selected or not.
kevin2106 kevin2106

2014/12/6

#
That looks like what i did now, i got it working with the code below:
 private boolean state = false;

public void boatSelect()
   {
       if (state == false)
        {  
            if (Greenfoot.mouseClicked(this)) 
            {  
            state = true;  
            } 
            
        } 
        else if (state == true) 
        {  
            setImage(new GreenfootImage("boat1Sel.png"));
            boatMovement(); 
            boatDeselect();
            //getWorld().addObject(icon1, getX(), getY());
        }  
   }
   public void boatDeselect()
   {
       if (this.state)
        {
            if (Greenfoot.mouseClicked(this))
            {
                state = false;
                setImage(new GreenfootImage("boat02-f.png"));
                return;
                //getWorld().removeObject(icon1);
                
            }
        }
   }
Thanks for the help !
sjoerdieman sjoerdieman

2014/12/7

#
its fun to see other people asking stuff about the same school project ;)
You need to login to post a reply.