Original Post
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();
}
}