[java] Problem with main game loop in J2ME

Started by
1 comment, last by markr 17 years, 4 months ago
Hi all I have asked about this problem not too long ago, but I am still having a hard time solving it. The thing is that ever since I added a main game loop to my game, the "Exit" button stopped responding... I think that the loop is taking too many resources of the system and thats why it doesn't "feel" the click on the button. Here is the code I wrote:

class MainGameClass implements Runnable
{
	GameDataClass GameData = new GameDataClass();
	Display disp;
	MainGameClass(Display mainDisplay)
	{
		disp = mainDisplay;
	}
	public void run()
	{
		GameData.initGame(disp);
		while (true)
		{
			GameData.clearScreen();
			GameData.checkGameEvents();
			GameData.drawScreen();
			try
			{
				Thread.sleep(10);
			}
			catch(Exception e)
			{
			}
		}
	}
}

public class MainClass extends MIDlet implements CommandListener
{
	public void startApp()
	{
		MainGameClass MainGame = new MainGameClass(Display.getDisplay(this));
		new Thread(MainGame).start();
	}
	public void pauseApp()
	{
	}
	public void destroyApp(boolean Unconditional)
	{
	}
	public void commandAction(Command c,Displayable d)
	{
		if (c.getCommandType()==Command.EXIT)
		{
			System.out.println("Exiting program!");
			destroyApp(true);
			notifyDestroyed();
		}
	}
}


As you can see, I have tried using the "Thread.sleep()" method but it doesn't help... I really need some help with this problem so lots of thanks in advance to all helpers :)
Don''t have one, sorry.
Advertisement
Try stopping the thread when you exit the game:
class MainGameClass implements Runnable{	GameDataClass GameData = new GameDataClass();	Display disp;        bool running;	MainGameClass(Display mainDisplay)	{		disp = mainDisplay;	}	public void run()	{		GameData.initGame(disp);                running = true;		while (running)		{			GameData.clearScreen();			GameData.checkGameEvents();			GameData.drawScreen();			try			{				Thread.sleep(10);			}			catch(Exception e)			{			}		}	}        public void stop()        {                running = false;        }}public class MainClass extends MIDlet implements CommandListener{        MainGameClass theGame;	public void startApp()	{		theGame = new MainGameClass(Display.getDisplay(this));		new Thread(theGame).start();	}	public void pauseApp()	{	}	public void destroyApp(boolean Unconditional)	{	}	public void commandAction(Command c,Displayable d)	{		if (c.getCommandType()==Command.EXIT)		{                        theGame.stop();			System.out.println("Exiting program!");			destroyApp(true);			notifyDestroyed();		}	}}


shmoove
I use a class which extends TimerTask, and calls my canvas's tick() and serviceRepaints() methods on each tick (tick handles all game logic and then does whatever painting is necessary)

The TimerTask also monitors a "running" flag in its run() method, and if this flag is cleared, it calls its own cancel() method which effectively stops the timer.

When the game needs to restart, I create a new GameTimerTask object and start it up- which then continues to run the game until I want to stop, when I set the flag again flagging it to stop.

This also means that I don't explicitly create any threads.

Mark

This topic is closed to new replies.

Advertisement