Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 1497 times)
0 Members and 1 Guest are viewing this topic.
Offline ProSilva

Junior Newbie





« Posted 2011-08-12 09: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
« Reply #1 - Posted 2011-08-12 11: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

Myomyomyo.
Offline ProSilva

Junior Newbie





« Reply #2 - Posted 2011-08-13 02: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
Offline philfrei
« Reply #3 - Posted 2011-08-13 04:09:54 »

http://www.java-gaming.org/index.php/board,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.


"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline theagentd
« Reply #4 - Posted 2011-08-13 12: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...

Myomyomyo.
Offline ProSilva

Junior Newbie





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

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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (22 views)
2014-07-24 01:59:36

Riven (20 views)
2014-07-23 21:16:32

Riven (17 views)
2014-07-23 21:07:15

Riven (20 views)
2014-07-23 20:56:16

ctomni231 (48 views)
2014-07-18 06:55:21

Zero Volt (44 views)
2014-07-17 23:47:54

danieldean (35 views)
2014-07-17 23:41:23

MustardPeter (38 views)
2014-07-16 23:30:00

Cero (53 views)
2014-07-16 00:42:17

Riven (52 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!