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

2011/10/14

Help with "mouseClicked()"

1
2
wzl19920913 wzl19920913

2011/10/14

#
Hello I'm a university student and I'm taking a course on programming with Greenfoot. I'm trying to write a chess game, basically while clicking "run button" and clicking a chess object, it will flip(face up if they were originally facing down) and be controlled by arrow keys. Since someone has replied similar questions, thanks to him because I borrowed his idea while writing the following program :) public void movement() { if (Greenfoot.isKeyDown("left")) { moveLeft(); state=0; } } private int state=0; public void act() { if (Greenfoot.mouseClicked(this)) { flip(); if (state == 0) { if (Greenfoot.mouseClicked(this)) { state = 1; } } else if (state == 1) { movement(); } } } The problem is that when I click on all the objects to flip them, and click a specific object again, the chess that satisfies the movement condition will not move left, but strangely starts to move after I clicking again. The source code seems to work well if the first "if" condition is removed. Is there any way I can select one object to control after flipping some chesses and then clicking it? Thank you very much!
danpost danpost

2011/10/14

#
I have a feeling that if you look closely at your bracketing within the act() method, you will come to understand why it is doing what it is doing. Step through the code in your mind and see if it doing what you want it to do. It currently is checking for mouse-click within code that was reached because you already found mouse-click. BTW, are you finding all clicked objects to be moving when an arrow key is pressed?
danpost danpost

2011/10/15

#
It also appears that once the objects starts moving, it stops! You have 'state' going back to zero (0) within the movement() method. First, lets say you named your world class 'World', and your object class 'Object'. Now, add an Object variable to World (in world class, add 'public static Object currentObject;'). Next, in the Object class, add 'if (Greenfoot.mouseClicked(this)) World.currentObject = this;'. Then, use 'if (this.equals(World.currentObject)) movement();'. This will give you movement only on the last clicked object.
wzl19920913 wzl19920913

2011/10/15

#
thanks a lot for your help. i made the following modification: public void act() { if (Greenfoot.mouseClicked(this)) { ChessWorld.currentChess = this; flip(); } if (this.equals(ChessWorld.currentChess)) { if (state == 0) { state = 1; } else if (state == 1) { movement(); } } } only the lask clicked chess moves, but it moves left as long as the moveLeft() condition is satisfied. i think the problem is probably here: if (this.equals(ChessWorld.currentChess)) , but don't know what i should do to fix it. is there anyway that the chess stops after one movement? thank you in advance! :)
danpost danpost

2011/10/15

#
My coding looked like this:
public void act() 
{
    if (Greenfoot.mouseClicked(this)) 
    {
        turn(180);
        Board.currentMan = this;
    }
    if (this.equals(Board.currentMan))
    {
        if (Greenfoot.isKeyDown("left") && getX() > 25) setLocation(getX() - 1, getY());
        if (Greenfoot.isKeyDown("right") && getX() < 375) setLocation(getX() + 1, getY());
        if (Greenfoot.isKeyDown("up") && getY() > 25) setLocation(getX(), getY() - 1);
        if (Greenfoot.isKeyDown("down") && getY() < 375) setLocation(getX(), getY() + 1);
    }
    else
    {
        setLocation(getX() - (getX() % 50) + 25, getY() - (getY() % 50) + 25);
     }
}
Almost identical to what you now have! But what exactly are you asking with the chess stopping after one movement? Is this something you want it to do? and how far is one movement? What does your moveLeft() method look like?
wzl19920913 wzl19920913

2011/10/15

#
thank you. the method is like this: public void moveLeft() { Chess c = (Chess)getOneObjectAtOffset(-1,0,Chess.class); if (c!=null) { if ( c.isFlipped==false || getChessRank()<c.chessRank) { setLocation(getX(),getY()); } else { setLocation(getX()-1,getY()); getWorld().removeObject(c); } } else { setLocation(getX()-1,getY()); } } what i want is that the method is called only once if i press left arrow key. now the chess just keeps moving until it is blocked by a higher ranked or unflipped chess.
danpost wrote...
My coding looked like this:
public void act() 
{
    if (Greenfoot.mouseClicked(this)) 
    {
        turn(180);
        Board.currentMan = this;
    }
    if (this.equals(Board.currentMan))
    {
        if (Greenfoot.isKeyDown("left") && getX() > 25) setLocation(getX() - 1, getY());
        if (Greenfoot.isKeyDown("right") && getX() < 375) setLocation(getX() + 1, getY());
        if (Greenfoot.isKeyDown("up") && getY() > 25) setLocation(getX(), getY() - 1);
        if (Greenfoot.isKeyDown("down") && getY() < 375) setLocation(getX(), getY() + 1);
    }
    else
    {
        setLocation(getX() - (getX() % 50) + 25, getY() - (getY() % 50) + 25);
     }
}
Almost identical to what you now have! But what exactly are you asking with the chess stopping after one movement? Is this something you want it to do? and how far is one movement? What does your moveLeft() method look like?
danpost danpost

2011/10/15

#
OK, you are using an 8 x 8 world (I was using a 400 x 400 world)! Instead of isKeyDown(String keyName), use the following in act():
String myKey = Greenfoot.getKey();
if (myKey != null)
{
    if ("left".equals(myKey) && getX() > 0) moveLeft();
    if ("right".equals(myKey) && getX() < 7) moveRight();
    if ("up".equals(myKey) && getY() > 0) moveUp();
    if ("down".equals(myKey) && getY() < 7) moveDown();
}
wzl19920913 wzl19920913

2011/10/15

#
amazing! that's exactly what i need, thank you so much~! but why..? isn't getKey() a method that returns the most recently used key? why it still moves even i didn't press anything before or i press right arrow key but the most recently used one is left arrow key? our professor will be surprised when he sees this code..coz he didn't teach us anything about that. thanks again, you've really helped me a lot :)
danpost danpost

2011/10/15

#
The method 'getKey()' will indeed return the name of a key that has been pressed AND released. Once Greenfoot has returned an actual keystroke with 'getKey()', that keystroke is history. The next call with getKey() will return 'null' until another keystroke takes place. That is the reason I used the String variable 'myKey', to store that keystroke and check to see if it is one of the keystrokes we want to act on. "why it still moves even i didn't press anything before or ..." Is something not yet correct in the actions of your objects? If so, I may need to see what you currently have in your Chess class.
wzl19920913 wzl19920913

2011/10/16

#
no, it works perfect... i misunderstood the function of getKey(), but now it's clear. thank you for your explicit explanation XD
danpost danpost

2011/10/16

#
Glad I could help! Check out my Chess scenario ChessWorld (scenario 2899). There are still two bugs that I know of: (1) the notation during pawn promotion to other than Queen is flawed and (2) allows castling (o-o or o-o-o) through check. These will be corrected in a later version. The scenario also comes with an alternate chess-like game called 'Kapture Chess'
lzw19920109 lzw19920109

2011/10/18

#
Hello,I am one of them learning GreenFoot and doing the same project. I have the same problem . My code is : public void act() { if ( Greenfoot.mouseClicked(this) ) { if (this.getFlipped() == false) { flip(); //flip the chess } else if (this.getFlipped() == true) { String myKey = Greenfoot.getKey(); if (myKey != null) { if ("left".equals(myKey) && getX() > 0) moveLeft(); if ("right".equals(myKey) && getX() < 7) moveRight(); if ("up".equals(myKey) && getY() > 0) moveUp(); if ("down".equals(myKey) && getY() < 7) moveDown(); } } } } } But I found that the chess would not move when pressing any direction bottom after clicking the chess;however,if I click the chess again,it moves. How can I correct these code in order to make the chess move one time by pressing movement key after I clicked the chess once?
danpost danpost

2011/10/18

#
The mouse click and the keystroke are two different actions and must be dealt with separately. As the code is written above you are asking 'if clicked and flipped, check keystrokes'. What you actually want is (1) 'if clicked and not flipped, then flip', (2) 'if flipped, then check keystrokes'
if (Greenfoot.mouseClicked(this)
{
    if (!isFlipped)  // the '!' means 'not' ('true' becomes 'false', and 'false' becomes 'rue')
    {
       flip();
    }
}
if (isFlipped)
{
    String myKey = Greenfoot.getKey();
    //  etc.
}
I think you will find that if you click more than one chess, that one of them will move, but not necessarily the one you intended. You will probaby need a world variable of type Chess to hold the last clicked chess called something like 'lastChessClicked' (In World class, but outside the methods (public static Chess lastChessClicked';) referred in Chess by (.lastChessClicked) (substitute with the name of your world). Then instead of having to flip the chess, just check to see 'if (this.equals(.lastChessClicked)'. Thy this:
if (Greenfoot.mouseClicked(this))
{
    World.lastChessClicked = this;
}
String myKey = Greenfoot.getKey();
if (this.equals(World.lastChessClicked) && myKey != null)
{
    //  check keystrokes here
}
Again, substitute the name of your world for 'World' and declare the Chess variable 'lastChessClicked' in your world class.
jonina jonina

2011/10/25

#
i have the same project of it and i have the similar problem. i have try to declare a var on the world and here is the code on the act method on the chess class. but the chess cant move at all. Thanks! code on the world: public class ChessWorld extends World { public static Chess lastChessClicked; code on the act method : public void act() { if (Greenfoot.mouseClicked(this) ) {if ( isFlipped == false) flip() ; else if ( isFlipped == true) ChessWorld.lastChessClicked = this; } String myKey = Greenfoot.getKey(); if (this.equals(ChessWorld.lastChessClicked) && myKey != null) { if ("left".equals(myKey) && getX() > 0) moveLeft(); if ("right".equals(myKey) && getX() < 7) moveRight(); if ("up".equals(myKey) && getY() > 0) moveUp(); if ("down".equals(myKey) && getY() < 7) moveDown(); } }
danpost danpost

2011/10/25

#
Did you write out the methods in Chess class for 'moveLeft()', 'moveRight()', 'moveUp()', and 'moveDown()'? What do they look like? And, your world is 8 x 8, right? @jonina, what action are you trying to achieve? From what I see, you want 'flip()' on first click; then, on second click 'select' (ChessWorld.lastChessClicked = this) and allow keystroke movement. Is that correct?
There are more replies on the next page.
1
2