Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with MouseClicked

 
Saara Pakarinen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, Games it is.
 
Saara Pakarinen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could have both MouseListeners point to a communal object, some kind of MouseInterpreter?
 
Saara Pakarinen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody for your kind advice. The game's working now. The problem was indeed a flat loop that's fixed now!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic