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

2011/10/14

Help with "mouseClicked()"

1
2
jonina jonina

2011/10/25

#
yes, i hv written the method, 'moveLeft()', 'moveRight()', 'moveUp()', and 'moveDown() and thew orld is 8 x 8. i want to achieve the same outcome as they stated above., Using the key (left, right, up, down) to move the chess for 1 step each time.
danpost danpost

2011/10/25

#
To achieve the following: if clicked and not flipped, flip; if clicked and flipped, select; and if selected and keystroke found, move; Use the following:
String myKey = Greenfoot.getKey();
if (this.equals(ChessWorld.lastChessClicked) && myKey != null)
{
    if ("up".equals(myKey) && getY() > 0) moveUp();
    if ("down".equals(myKey) && getY() < 7) moveDown();
    if ("left".equals(myKey) && getX() > 0) moveLeft();
    if ("right".equals(myKey) && getX() < 7) moveRight();
}
if (Greenfoot.mouseClicked(this))
{
    if (isFlipped)
    {
        ChessWorld.lastChessClicked = this;
    }
    else
    {
        flip();
        isFlipped = true;
    }
}
I added 'isFlipped = true;' to show you this needs to be done (it can and should be in the 'flip()' method). You may also want to add 'ChessWorld.lastChessClicked = null;' with it, to de-select last possible mover when something else is clicked on.
You need to login to post a reply.
1
2