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

2011/10/12

How to use mousePressed(obj)?

MichaelC MichaelC

2011/10/12

#
I'm trying to get Greenfoot.mousePressed(object) to work. I want to make it so whenever I click an object in the world, I want to make it move. Here's my code
 if (Greenfoot.mousePressed(WhitePawn.class) == true)
        {
            movement();
        }
I also want to make it so that once I select an object, I can press a key to make it move forward Here's my code for that, it would work with the code above
 public void movement()
    {
     if (Greenfoot.isKeyDown("w") == true)
        {
         setLocation(getX(), getY()-75);
         endTurn();
        }
    } 
MichaelC MichaelC

2011/10/12

#
Thanks a lot in advance for any help!
mjrb4 mjrb4

2011/10/12

#
The issue seems to be that your movement method only does anything if the w key is held down - so it's called when you press the mouse on WhitePawn, but unless the w key is held down will exit straight away.
MichaelC MichaelC

2011/10/13

#
Is there any way around this, maybe using another method? I'm trying to make a working chess game, and I want a way to use programming to move the pieces as opposed to just pausing the program and clicking and dragging where you want to move your piece. I know there are a few methods that let you click and drag while the program's running, but I'd prefer to not use those as I can't think of a way to stop people from making illegal moves and the pieces also won't be centered on their square. Any help?
kiarocks kiarocks

2011/10/13

#
you can use a while loop like so
if (Greenfoot.mousePressed(WhitePawn.class) == true)  
       {  
        while(Greenfoot.isKeyDown("w") != true
           {
           movement();  
           }
        }    


public void movement()  
   {  
    if (Greenfoot.isKeyDown("w") == true)  
       {  
        setLocation(getX(), getY()-75);  
        endTurn();  
       }  
davmac davmac

2011/10/13

#
It sounds to me like you have two distinct states: 1. No object selected (or rather: this object is not selected) 2. Object selected, waiting for keypress In Greenfoot you typically implement this using a variable keeps track of the current state, and change the behaviour (in the act method) depending on the current state, eg:
private int state = 0;

public void act()
{
    if (state == 0) {
        if (Greenfoot.mousePressed(this) {
            state = 1;
        }
    } else if (state == 1) {
        movement();
    }
}

public void movement()
{
    if (Greenfot.isKeyDown("w")) {
        setLocation(getX(), getY() - 75);
        endTurn();
        state = 0;
    }
}
There are still some issues with this - for instance, it doesn't stop more than one object being selected. To do that you'd need to co-ordinate somehow (probably through the world). Note there was an error in your code: you shouldn't pass a class to Greenfoot.mousePressed(...) - you must pass an actor or the world. I've corrected that.
MichaelC MichaelC

2011/10/13

#
Awesome, this is my first programming class and it's only been about 4 weeks so far, so I wasn't sure what to put inside those parenthesis, but what you've done makes sense. Thanks a lot!
Duta Duta

2011/10/13

#
Personally I would have used a boolean (something along the lines of isSelected), but the solutions above work too.
You need to login to post a reply.