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. ??? ???I’m willing to email the person willing to help, the game in full. It’s for my School PAT Project.
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.
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.
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, to him.
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.
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…
Thanks T-Man…peace out