Tutorials

Tutorials


Libgdx Specific


LWGJL Specific


Engines, Frameworks, Libraries


Source


Books

Holy crap this is amazing! Bookmarked

Hours and hours of digging.

You sir deserve an appreciation :slight_smile:

My first one! :slight_smile:

Whoa?!? How did I get 9? I help people too much :stuck_out_tongue:

You deserve a lot more then one for that! I gave you your second!

Hear ya go, I gave you another one. Which brings your total to…3 :slight_smile:

No libgdx!? Fail! :wink:

Thanks guys! I’ll keep updating it. I’ll dig up some libgdx. I gave you 2 or 3 because you answered like every thread I posted in for like a week.

Now with the only books anyone ever mentions!

Thanks! Appreciated!

For learning Java in general I’d recommend Thinking in Java for noobs. If you have a little programming experience already, this online book moves a little faster:
http://math.hws.edu/javanotes/

The link titled “Turk4n’s game tutorials” can’t be accessed

Great compilation of information.

I’m stuck on the GameDev Java Game Programming 2 link. I figured out that I was getting an error because the author forgot to import java.awt.*;, but now it appears that my repaint() function isn’t working correctly. The count for i is not increasing and it just stays stuck at i = 0.

Does anyone know why this is? Here’s his code below:

import java.applet.*;
import java.awt.*;

public class SampleThread extends Applet implements Runnable
{
	Thread t;
	int i;
	
	public void init()
	{
		t = new Thread();
		t.start();
		
		i = 0;
	}
	
	public void run()
	{
		while(true)
		{
			i++;
			repaint();
			
			try{
				t.sleep(1000);
			} catch (InterruptedException e) { ; }
		}
	}
	
	public void paint(Graphics g)
	{
		g.drawString("i = "+i, 10, 20);
	}
}

And here’s the html page running it:

<HTML>
<HEAD>
<TITLE>Sample Thread Applet</TITLE>
</HEAD>
<BODY>
<CENTER>
<H1>Sample Thread Applet</H1>
<APPLET CODE="SampleThread.class">
</APPLET>
</BODY>
</HTML>

Thanks to anyone who can help me!

You should have started a new thread instead of hijacking an old thread.

But to answer your question, try putting the word “volatile” before the declaration of “int i;”

This happens because usually the CPU caches data that is in use often, instead of accessing RAM repeatedly. This causes variables to not be in sync, especially here since you have a thread updating “i” and another thread, the EDT, repainting the screen. Setting a variable as “volatile” makes sure it is updated among all threads.

Sorry about that. If a mod can take my first post and create a new thread for it and the responding posts, that would be awesome.

Regarding adding volatile to the declaration of int i, that didn’t seem to work. Nothing seemed to change.

Just so I’m clear in my understanding, the thread I created is updating the value of i, but another thread exclusive to handling events in the AWT is created when calling repaint(), correct?

Right but it is not created, it is a queue that dispatches events, hence its called the Event Dispatching Thread, EDT.
And also remove that call to “i = 0” that will set “i” back to 0 after the tread starts incrementing it.

I commented out the initialization of i, but still no dice. I shouldn’t have to use a lock, should I?

When you create the thread, you need to include “this”

t = new Thread(this);

That’s the only way the compiler knows to use the run method you put in.

Also, when you sleep, it might be better to use this form:

Thread.sleep(1000);

The “i = 0;” in init doesn’t hurt anything. The init code is only executed once. But it is not needed because when you declare i as an instance variable, the default value of 0 is assigned.