Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Gaming and Threads  RSS feed

 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am writing my very first game and have run into a problem. I am getting inconsistent results while playing the game. Overall, I believe my problem is due to poor design. I have a repaint() method inside of a thread which keeps all of the animation going. I think what is happening is that my screen is being repainted before certain variables have been set, because instead of declaring my variables private and using get and set methods I simply clobber the old variable from another inner class. I hope I am making some sense here. Anyway, I was wondering if someone has had some gaming experience and could lead me in the right direction. Do I need to go back and provide methods to get and set variables or am I overlooking something else. Thanks for your input!!!
P.S. Anyone who provides some valuable input here will get a free copy of this game when I am done. It is pretty cool.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, thats a familiar problem. I ran into it when doing a applet that presented an animated baseball game. My solution:
The Thread that calculated the next iteration of variables called repaint and then called wait()
In the paint method, after the field was repainted, it called notify() - this wakes up the animation Thread for the next cycle.
This ensures that the painting and animating Threads take turns.
(Naturally I am leaving out all the synchronization details)
Bill

------------------
author of:
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill,
Thanks for your response! So if I understand you correctly, I do not necessarily need to provide get and set methods. But simply, adding wait and notify methods should take care of my problem. Thanks again!
By the way, I do have your Exam Cram book which I have found very useful.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!