Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (652)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  My java Game doesn't run Threads properly!!! Help  (Read 1809 times)
0 Members and 1 Guest are viewing this topic.
Offline ProSilva

Junior Newbie

« Posted 2011-08-12 07:08:10 »

I have got 2class which I would like to run simultineously. In one class I have got an action figure to move around and punch, accorroding to key pressed (e.g a - left punch, s - right punch). In the other class I have an extention of the first class, this only has a punching bag which moves around the screen waiting to be punched. I tried using a thread to run both at the same time, but it either flickers like crazy or does not run at all. Huh HuhI'm willing to email the person willing to help, the game in full. It's for my School PAT Project.

Offline theagentd

« JGO Bitwise Duke »

Medals: 525
Projects: 3
Exp: 8 years

« Reply #1 - Posted 2011-08-12 09:21:07 »

Threading sprites (game objects) is a very bad idea. Having a thread for each sprite will cause many problems with for example collision detection (in your case punches). I don't know if this is what you are doing though, you were a little unclear...
Callbacks from keyboard events in Java2D/Swing (the KeyListener functions) are called from another thread, namely the AWT event thread. You should probably not do much more than save the key press here. Do not do health checks here (EDIT: I mean checking if your enemy/punchbag dies). Don't even do the collision testing of your punch here. I recommend keeping a boolean variable for the key state of each key you need (boolean isPunchLeftDown, isPunchRightDown; for example). The only thing you do in keyPressed() and keyReleased() is modify these variables if the correct button is pressed. Then check these variables in your game loop. I'll post some code if you don't understand.
Concerning flickering, I believe you are rendering directly to your JFrame/Frame/Applet or whatever you're using. The flickering occurs because your computer screen might update it's content while you are halfway through drawing. The solution is double buffering. Basically create a BufferedImage and render your game's graphics to it. When you are done copy the image (your back buffer) to your Frame/JFrame/Applet/whatever. Again, I'll post code if you want me to.  Tongue
As you say your game randomly does not work, I believe you are experiencing a phenomena known as "deadlocking". It is when two or more threads are waiting for each other and they therefore freeze. This usually happens accidentally due to one thread modifying something another thread uses during a time when the variable is not expected to be modified. I don't know how much programming experience you have, but multithreading is fairly advanced. Synchronizing threads so they do not randomly freeze or crash is pretty hard and VERY hard to debug as the program can run fine until it seems to freeze without any clear reason. I recommend avoid multithreading as much as possible, but as you're using Java2D, you will have to split up your rendering and your game loop. The easiest way to avoid problems and synchronization is to keep clear rules of which variables a thread can modify. For a game loop thread and Java2D thread, it is pretty easy. The rendering thread shouldn't modify anything from the current game state as it should only read the position, color and/or images of your objects to be able to draw them.
It would be better if we kept the discussion public, as other people can read our posts and hopefully learn from them.  Wink

Offline ProSilva

Junior Newbie

« Reply #2 - Posted 2011-08-13 00:17:32 »

I hear all you saying, but it's gonna take me forever to get it working with the knowledge i have. I will email you all my coding, if you wish to take a look at it. Referecing of code will be to you, and I will take no credit for your work, I would just like to see my game work! If I may have your email address. If you having doubts, I will give you my teachers email address, and you can return the code you worked on, Smiley to him.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online philfrei
« Reply #3 - Posted 2011-08-13 02:09:54 »,65.0.html

This link has some good, readable examples of a game loop. If you are just animating two objects, and are a beginner, something from here should be ideal for you.

"We all secretly believe we are right about everything and, by extension, we are all wrong." W. Storr, The Unpersuadables
Offline theagentd

« JGO Bitwise Duke »

Medals: 525
Projects: 3
Exp: 8 years

« Reply #4 - Posted 2011-08-13 10:06:09 »

Sorry, ProSilva. I don't really want to do your game for you...  I don't think you would gain much from looking at the code either way. What you need (if you want to learn more that is xd) are tutorials, which Philfrei has kindly supplied for you. I didn't even know those existed.
Start at the game loop tutorial and when you feel that you understand the basics of game loops continue to the basic game tutorial. It's not that advanced, but the time you need might depend on your programming skills...

Offline ProSilva

Junior Newbie

« Reply #5 - Posted 2011-08-14 12:46:45 »

Thanks T-Man....peace out
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

SHC (21 views)
2015-08-01 03:58:20

Jesse (19 views)
2015-07-29 04:35:27

Riven (39 views)
2015-07-27 16:38:00

Riven (21 views)
2015-07-27 15:35:20

Riven (24 views)
2015-07-27 12:26:13

Riven (14 views)
2015-07-27 12:23:39

BurntPizza (35 views)
2015-07-25 00:14:37

BurntPizza (46 views)
2015-07-24 22:06:39

BurntPizza (28 views)
2015-07-24 06:06:53

NoxInc (36 views)
2015-07-22 22:16:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!