aspose file tools*
The moose likes Game Development and the fly likes problem with MouseClicked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "problem with MouseClicked" Watch "problem with MouseClicked" New topic
Author

problem with MouseClicked

Saara Pakarinen
Greenhorn

Joined: Sep 05, 2004
Posts: 7
Hi,

I'm almost finished with a TBS (turn-based) game that still needs the actual game method. I'm stuck, because I don't know how to make computer and human player take turns.

I have a class that implements a MouseListener. It has several methods in it that are done if mouse is clicked (mouseClicked(){}).
The MouseClicked has a state field that defines the method that's run when the mouse is clicked. All of these methods including the MouseClicked method work perfectly.

When a human player clicks an area something is done, the mousethread ends and another method that the computer does should start. It never does. Either the mouseClicked is totally ignored or the game enters the class that implements the MouseListener and carries out MouseClicked method and never returns to my main class. How is this possible? Can anyone help? I'd appreciate any hints.

I have very little experience with MouseListener and I'm really stuck with my problem.
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
that's precisely how the mouseClicked works; it is called when the mouse is clicked by the canvas you added the listener to, and then the method terminates. If you want a computer to run after the mouse, you have a couple of options. The most obvious is to have a call to run the computer's turn at the end of the mouseClicked method. If this is fine for you, go for it... If you are a style weirdo like me, this set-up may offend you, because who is the MouseListener to call the computer AI?! That's the job of the main program. In that instance, you could have the mouseClicked method alter a boolean, something like humanTurn to false. When the game goes through it's next loop, it will read this boolean and say, "This time, run the computer AI," afterwhich the boolean will return to true. The mouseListener would only pay attention to input if humanTurn were true.

But as I said, if you can get away with the first way, it's alot simpler. Just not as asthetically pleasing to my eye.


final note: perhaps this thread would fit under games forum?


I've heard it takes forever to grow a woman from the ground
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

OK, Games it is.


[Jess in Action][AskingGoodQuestions]
Saara Pakarinen
Greenhorn

Joined: Sep 05, 2004
Posts: 7
Thanks for your answer.
I'm new to this forum so I wasn't sure where to
put this kind of question. Sorry.

The second option you suggested I've tried already and that's where it all goes wrong. If I put a loop in the main game maethod the game gets stuck. It won't let me click the canvas and the mouseClicked is never executed. That's why the boolean is never altered and the loop is never-ending.

The first option seems like my only chance now (which means a lot more work since I have to alter my game structure radically).
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Saara,

Is the main part of your code small enough to post (say ~50 lines)? If so please post it wrapped in the UBB [C0DE] and [/C0DE] tags to preserve the formatting. It sounds to me like you're doing something iffy (e.g. a flat loop or Thread.sleep()) in the event dispatch thread and the screen painting's never getting a look-in.

Jules
Saara Pakarinen
Greenhorn

Joined: Sep 05, 2004
Posts: 7
The code is in parts in different classes and it's too long.

The problem with this case is that I have two separate gameboards that have separate MouseListeners. With only one gameboard I'd just make the MouseClicked run the AI.
I have an abstract class called a GameBoard (extends JPanel, implements MouseListener).

Both the human player and the computer player get this kind of GameBoard Object. So the human GameBoard can't do changes to the computer GameBoard or the other way round.

That's why I have to run their methods separately.

I've never heard of a flat loop. What's that?

What I've noticed so far is that when the MouseClicked is started it never returns to the main method and the game never continues.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
A flat loop is a loop that doesn't yield or sleep or block in any way such that the CPU is maxed out and no other threads/processes get a look-in.

Good luck with finding your problem.

Jules
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
you could have both MouseListeners point to a communal object, some kind of MouseInterpreter?
Saara Pakarinen
Greenhorn

Joined: Sep 05, 2004
Posts: 7
Joseph: I've tried that one as well, but since my method never returns it doesn't work. It just executes the mouseClicked if I set the GameBoard class to execute it and reacts to only mouseClicks or button clicks after that.

I guess I have some sort of a flat loop then since when the human player method (that's started by the MouseClicked) ends it just stops there. I can however make the human GameBoard do something else through mouseClicked, but not for the other GameBoard (the computer).

I call the MouseClicked so that a gamemethod from my main class (the JApplet) executes a method that sets a value in my GameBoard class to a position which tells the MouseClicked that next time mouse is Clicked execute the human player method.

If I run the setMouseClickedPosition once it executes perfectly and does all the necessary changes to the gameboard. Then if I press the mouse again, it executes the same method again. It kind of stays there in the GameBoard class, never stopping or returning.

I'm starting to think that this is a mission impossible and that I've done the whole game a completely wrong way. Wish I'd tested that MouseClicked can be called and executed from another class like any other method (that end and then return to the main class) before starting the coding process.
Saara Pakarinen
Greenhorn

Joined: Sep 05, 2004
Posts: 7
Thanks everybody for your kind advice. The game's working now. The problem was indeed a flat loop that's fixed now!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with MouseClicked