Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Thread consistency?

Started by Goguigo Aug 26, 2006 at 12:06 AM 2 replies 550+ views
Original Post
Goguigo
Goguigo
Hey fellows. :) I have been working on a simple appy. It has a main class that in first hand calls an Intro class (it displays several frames for laike 3 seconds) on a thread. After that it calls a menu class, that can call 3 diferent classes. The thing is, i have done this like all J2ME tutorials said... the same "structure"... but i get a choppy fealing in some devices. May it be the thread consistency? I dont implement it, but several tutorials include a Thread.skeep(delay) where delay = 20;... may be my problem this thing? The resources? i'm using realy simple images. Sprites here and there, string array for text, the draw image method do a lot of stuff... a lot of that stuff is calculate over and over the position of things. What can i do? An example of the main menu code:
[source code=javax]
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

public class MainMenu extends GameCanvas implements Runnable 
{
	private boolean isPlay;
	
	//Nuevo
	private int opcionSelect=0;
	
	//Porsiacaso
	private MainBase padre;
	
	//OPCIONES STRING
	public String opciones[]=//lineas de texto
	{
			"Comenzar","Creditos","Salir","Cargando..."
	};
	
	Font fMenu = Font.getFont (Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN,Font.SIZE_MEDIUM);// Es el tipo de letra para el texto del menu.

	
	//CREAMOS EL FONDO DEL MENU
	public Image fondoMenu = null;//se crea la variable imagen
	public Sprite spriteMenu=null;
	public Image selMenu = null;//flechitas
	private Sprite spriteSel;//objeto sprite de las flechitas
	
	//variables de soporte
	private int a=getWidth()/2;//ancho de pantalla
	private int b=getHeight()/2;//alto de pantalla
	public int alfa=0;//variable soporte para input
	private int aa=0;
	private int aa=0;
	private int aa=0;
	private int aa=0;
	private int aa=0;
	private int aa=0;
	private int aa=0;
	private int aa=0;
	private int aa=0;
	private int aa=0;
	
	// Constructor and initialization
	public MainMenu(MainBase padre) 
	{
		super(true);
		setFullScreenMode(true);//determinamos que sea vea todo en fullscreen
		this.padre = padre;
	}
	public void iniciar()//inicializa los componentes de juego, variables, imágenes, etc
	{
		try //cargamos primera página del comic
		{
			fondoMenu = Image.createImage("/fi.png");//cargamos el archivo
			spriteMenu =new Sprite(fondoMenu,128,128);//dimensiona los frames del comic
			selMenu = Image.createImage("/sel.png");//cargamos el archivo
			spriteSel =new Sprite(selMenu,16,16);//dimensiona los frames del comic
		}
		catch (Exception e)
		{}
	}

	public void start()
	{
		isPlay = true;
		Thread t = new Thread(this);
		t.start();
	}
	public void stop() { isPlay = false; }

	public void run() 
	{
		Graphics g = getGraphics();
	    g.setColor(0,0,0);
	    g.fillRect(0, 0, a, b);
	    g.setColor(255,255,255);
		g.drawString("Cargando...", a/2, b/2,  Graphics.HCENTER|Graphics.TOP);
		repaint();
		iniciar();
		while (isPlay == true) 
		{
			input();
			while(alfa!=0)//mientras se este apretando el boton
			{
				alfa=getKeyStates();//vemos si esta apretando algun boton
			}//si es así, continuar con el ciclo while y bloquear el paso hasta que no se aprete nada
			alfa=0;
			computar();
			drawScreen(g);
			try { Thread.sleep(100); }
			catch (InterruptedException ie) {}
		}
		limpiar();
		padre.opcion = opcionSelect;
		padre.isOut = false;
	}
	
	public void computar()
	{
		if (opcionSelect>2)
			opcionSelect=0;
		if (opcionSelect<0)
			opcionSelect=2;
	}
	
	//LIMPIAMOS LA MEMORIA
	public void limpiar()
	{
		fondoMenu=null;
		spriteMenu=null;
		selMenu=null;
		spriteSel=null;

	}

	private void input() 
	{		
		int keyStates = getKeyStates();
		alfa=getKeyStates();
		// Arriba
		if ((keyStates == UP_PRESSED)|(keyStates ==  KEY_NUM2))
			opcionSelect--;	
		
		// Abajo
		if ((keyStates== DOWN_PRESSED)|(keyStates ==  KEY_NUM8))
			opcionSelect++;
		
		//	Accion
		if ((keyStates == FIRE_PRESSED)|(keyStates ==  KEY_NUM5))
			isPlay=false;
	}

	private void drawScreen(Graphics g) 
	{
		g.setFont(fMenu);//determinamos que se usará la fuente de menu
		//Nuevo
		//dibujamos fondo
		g.setColor(0,0,0);
		g.fillRect(0,0,400,400);
		spriteMenu.setFrame(0);//verificamos que tipo de flecha debemos usar
		spriteMenu.setPosition(0,0);//definimos la posición de la flecha con las coordenadas dadas
		spriteMenu.paint(g);//dibujamos la flecha
		try 
		{
				switch (opcionSelect)
				{
				case 0:
					spriteSel.setFrame(4);//se activa el ticket
					spriteSel.setPosition(a-60,b-20);//fijamos la posición del acompañamiento
					spriteSel.paint(g);//dibujamos el ticket de acompañamiento
				    g.setColor(255,0,0);
					g.drawString(opciones[0], a,b-20,  Graphics.HCENTER|Graphics.TOP);
				    g.setColor(255,255,255);
					g.drawString(opciones[1], a,b,  Graphics.HCENTER|Graphics.TOP);
				    g.setColor(255,255,255);
					g.drawString(opciones[2], a,b+20,  Graphics.HCENTER|Graphics.TOP);
					break;
				case 1:
					spriteSel.setFrame(4);//se activa el ticket
					spriteSel.setPosition(a-60,b);//fijamos la posición del acompañamiento
					spriteSel.paint(g);//dibujamos el ticket de acompañamiento
				    g.setColor(255,255,255);
					g.drawString(opciones[0], a,b-20,  Graphics.HCENTER|Graphics.TOP);
				    g.setColor(255,0,0);
					g.drawString(opciones[1], a,b,  Graphics.HCENTER|Graphics.TOP);
				    g.setColor(255,255,255);
					g.drawString(opciones[2], a,b+20,  Graphics.HCENTER|Graphics.TOP);
					break;
				case 2:
					spriteSel.setFrame(4);//se activa el ticket
					spriteSel.setPosition(a-60,b+20);//fijamos la posición del acompañamiento
					spriteSel.paint(g);//dibujamos el ticket de acompañamiento
				    g.setColor(255,255,255);
					g.drawString(opciones[0], a,b-20,  Graphics.HCENTER|Graphics.TOP);
				    g.setColor(255,255,255);
					g.drawString(opciones[1], a,b,  Graphics.HCENTER|Graphics.TOP);
				    g.setColor(255,0,0);
					g.drawString(opciones[2], a,b+20,  Graphics.HCENTER|Graphics.TOP);
					break;
				}	
		}
		catch (Exception e)
		{}
		flushGraphics();
	}
}

shmoove
shmoove
I see you are blocking your main thread until the user releases the key. Maybe you should try some other method of limiting the menu movement that doesn't block the thread. Something like having a lastAlfa variable that remembers the value from the last time around and if alfa == lastAlfa then input() does nothing.

shmoove
Goguigo
Goguigo
Thanks again Shmoove :)

I implimented an input method that dows what you rold me and worked fine. :) THo i havent had the chance to tried on a phone yet.

THanks. Any other advice about the code optimization?

Thanks
Goguigo
Goguigo
mmmh, i tried the app on a mobile handset( 3220 nokia)... to use the control path for option select and "fire", i need to keep te button pressed for like 1 or 2 secs. :(

Here's the code i implimented:

[source code=javax]import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;public class MainMenu extends GameCanvas implements Runnable {	private boolean isPlay;		//Nuevo	private int opcionSelect=0;	private int keyStates=0;		//Porsiacaso	private MainBase padre;		//OPCIONES STRING	public String opciones[]=//lineas de texto	{			"Comenzar","Creditos","Salir","Cargando..."	};		Font fMenu = Font.getFont (Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN,Font.SIZE_MEDIUM);// Es el tipo de letra para el texto del menu.		//CREAMOS EL FONDO DEL MENU	public Image fondoMenu = null;//se crea la variable imagen	public Sprite spriteMenu=null;	public Image selMenu = null;//flechitas	private Sprite spriteSel;//objeto sprite de las flechitas		//variables de soporte	private int alfa=0;//variable soporte para input	private int beta=0;		// Constructor and initialization	public MainMenu(MainBase padre) 	{		super(true);		setFullScreenMode(true);//determinamos que sea vea todo en fullscreen		this.padre = padre;	}	public void iniciar()//inicializa los componentes de juego, variables, imágenes, etc	{		try //cargamos primera página del comic		{			fondoMenu = Image.createImage("/fi.png");//cargamos el archivo			spriteMenu =new Sprite(fondoMenu,128,128);//dimensiona los frames del comic			selMenu = Image.createImage("/sel.png");//cargamos el archivo			spriteSel =new Sprite(selMenu,16,16);//dimensiona los frames del comic		}		catch (Exception e)		{}	}	public void start()	{		isPlay = true;		Thread t = new Thread(this);		t.start();	}	public void stop() { isPlay = false; }	public void run() 	{		Graphics g = getGraphics();	    g.setColor(0,0,0);	    g.fillRect(0, 0, getWidth(), getHeight());	    g.setColor(255,255,255);		g.drawString(opciones[3], getWidth()/2, getHeight()/2,  Graphics.HCENTER|Graphics.TOP);		repaint();		iniciar();		while (isPlay == true) 		{			alfa=getKeyStates();			if(beta!=alfa)			{				input();			}			beta=alfa;			computar();			drawScreen(g);			try { Thread.sleep(20); }			catch (InterruptedException ie) {}		}		limpiar();		padre.opcion = opcionSelect;		padre.isOut = false;	}		public void computar()	{		if (opcionSelect>2)			opcionSelect=0;		if (opcionSelect<0)			opcionSelect=2;	}		//LIMPIAMOS LA MEMORIA	public void limpiar()	{		fondoMenu=null;		spriteMenu=null;		selMenu=null;		spriteSel=null;	}	private void input() //metodo para tomar el input del usuario	{				keyStates = getKeyStates();		if (keyStates == UP_PRESSED)			opcionSelect--;			// Abajo		if (keyStates == DOWN_PRESSED)			opcionSelect++;		//	Accion		if (keyStates == FIRE_PRESSED)				isPlay=false;	}	private void drawScreen(Graphics g) 	{		g.setFont(fMenu);//determinamos que se usará la fuente de menu		//Nuevo		//dibujamos fondo		g.setColor(0,0,0);		g.fillRect(0,0,400,400);		spriteMenu.setFrame(0);//verificamos que tipo de flecha debemos usar		spriteMenu.setPosition(getWidth()/2-64,getHeight()/2-64);//definimos la posición de la flecha con las coordenadas dadas		spriteMenu.paint(g);//dibujamos la flecha		try 		{				switch (opcionSelect)				{				case 0:					spriteSel.setFrame(4);//se activa el ticket					spriteSel.setPosition(getWidth()/2-60,getHeight()/2-20);//fijamos la posición del acompañamiento					spriteSel.paint(g);//dibujamos el ticket de acompañamiento				    g.setColor(255,0,0);					g.drawString(opciones[0], getWidth()/2,getHeight()/2-20,  Graphics.HCENTER|Graphics.TOP);				    g.setColor(255,255,255);					g.drawString(opciones[1], getWidth()/2,getHeight()/2,  Graphics.HCENTER|Graphics.TOP);				    g.setColor(255,255,255);					g.drawString(opciones[2], getWidth()/2,getHeight()/2+20,  Graphics.HCENTER|Graphics.TOP);					break;				case 1:					spriteSel.setFrame(4);//se activa el ticket					spriteSel.setPosition(getWidth()/2-60,getHeight()/2);//fijamos la posición del acompañamiento					spriteSel.paint(g);//dibujamos el ticket de acompañamiento				    g.setColor(255,255,255);					g.drawString(opciones[0], getWidth()/2,getHeight()/2-20,  Graphics.HCENTER|Graphics.TOP);				    g.setColor(255,0,0);					g.drawString(opciones[1], getWidth()/2,getHeight()/2,  Graphics.HCENTER|Graphics.TOP);				    g.setColor(255,255,255);					g.drawString(opciones[2], getWidth()/2,getHeight()/2+20,  Graphics.HCENTER|Graphics.TOP);					break;				case 2:					spriteSel.setFrame(4);//se activa el ticket					spriteSel.setPosition(getWidth()/2-60,getHeight()/2+20);//fijamos la posición del acompañamiento					spriteSel.paint(g);//dibujamos el ticket de acompañamiento				    g.setColor(255,255,255);					g.drawString(opciones[0], getWidth()/2,getHeight()/2-20,  Graphics.HCENTER|Graphics.TOP);				    g.setColor(255,255,255);					g.drawString(opciones[1], getWidth()/2,getHeight()/2,  Graphics.HCENTER|Graphics.TOP);				    g.setColor(255,0,0);					g.drawString(opciones[2], getWidth()/2,getHeight()/2+20,  Graphics.HCENTER|Graphics.TOP);					break;				}			}		catch (Exception e)		{}		flushGraphics();	}}


I don't know whats wrong. :( I'll keep working on it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.