i have case when i want do action in different time.
when i press LEFT and RIGHT i want do void keyPress, but when i press UP and DOWN i want do laser.scrollup or scrolldown.
i added boolean navigatorEnd
navigatorEnd = false
if i press up and down
action == Canvas.FIRE
end
if (navigatorEnd = true)
keyPressed()
end
but the problem is after i keyPress canvas.LEFT or Right, keypress doesn't work. and only execute action == Canvas.FIRE
please help me
public void dealKey(int keyCode){
if(keyCode==0) return;
int action = 0;
try{
action = getGameAction(keyCode);
}catch(Exception e){
e.printStackTrace();
}
if(keyCode == Canvas.KEY_NUM2 || action == Canvas.UP){
NavigatorEnd =true;
laser.scrollUp();
}else if(keyCode == Canvas.KEY_NUM8 || action == Canvas.DOWN){
NavigatorEnd =true;
laser.scrollDown();
}else if(keyCode == Canvas.KEY_NUM4 || action == Canvas.LEFT){
NavigatorEnd =false;
if (index == 0) {
index = 5;
} else {
index = index - 1;
}
}else if(keyCode == Canvas.KEY_NUM6 || action == Canvas.RIGHT){
NavigatorEnd =false;
if (index == 5) {
index = 0;
} else {
index = index + 1;
}
}else if(keyCode == Canvas.KEY_NUM5 || action == Canvas.FIRE){
if(NavigatorEnd =true){
laser.dealHotSpot();
}
}
}
int keyCode;
public void keyReleased(int keyCode) {
this.keyCode = 0;
}
public void keyPressed(int keyCode) {
this.keyCode = keyCode;
String block2;
if (NavigatorEnd == false) {
switch (getNavigatorAction(keyCode)) {
// aksi untuk tombol tengah / center selection (CENTER)----------------------------------
case FIRE:
if (symbols[index] == ""){
symbols[index] = playerSymbol;
if(winnerExist(symbols).equals("")){
boolean flag = false;
for(int i=0; i<symbols.length; i++){
if(symbols[i].equals("")){
flag = true;
}
}
if(flag==true){
switch(computerLevel){
case 0:
Random rnd0 = new Random();
int tmpX0=0;
for(;;){
//tmpX0 = rnd0.nextInt(8);
if(symbols[tmpX0].equals("")){
symbols[tmpX0]=computerSymbol;
midlet.display.setCurrent(form);
break;
}
}
break;
case 1:
String res1 = winningChance();
if(!res1.equals("")){
symbols[Integer.parseInt(res1)] = computerSymbol;
}else{
Random rnd1 = new Random();
int tmpX1=0;
for(;;){
//tmpX1=rnd1.nextInt(8);
if(symbols[tmpX1].equals("")){
symbols[tmpX1]=computerSymbol;
break;
}
}
}
break;
case 2:
block2 = blockChance();
if(!block2.equals("")){
symbols[Integer.parseInt(block2)] = computerSymbol;
}else{
String res2 = winningChance();
if(!res2.equals("")){
symbols[Integer.parseInt(res2)] = computerSymbol;
}else{
Random rnd2 = new Random();
int tmpX2=0;
for(;;){
// tmpX2=rnd2.nextInt(8);
if (symbols[tmpX2].equals("")){
symbols[tmpX2]=computerSymbol;
break;
}
}
}
}
break;
case 3:
if(symbols[4].equals("")){
symbols[4] = computerSymbol;
computerLevel=2;
break;
}else{
Random rnd2 = new Random();
int tmpX2=0;
for(;;){
// tmpX2=rnd2.nextInt(8);
if (symbols[tmpX2].equals("")){
symbols[tmpX2]=computerSymbol;
computerLevel=2;
break;
}
}
}
break;
case 4:
if(symbols[4].equals("")){
symbols[4] = computerSymbol;
computerLevel=2;
break;
}else if(symbols[0].equals("")){
symbols[0]=computerSymbol;
break;
}else if(symbols[2].equals("")){
symbols[2]=computerSymbol;
break;
}else if(symbols[6].equals("")){
symbols[6]=computerSymbol;
break;
}else if(symbols[8].equals("")){
symbols[8]=computerSymbol;
break;
}else{
Random rnd2 = new Random();
int tmpX2=0;
for(;;){
// tmpX2=rnd2.nextInt(4);
if (symbols[sudut[tmpX2-1]].equals("")){
symbols[sudut[tmpX2-1]]=computerSymbol;
computerLevel=2;
break;
}
}
}
break;
default:
break;
}
}
}
}
break;
//case UP:
// browser.scrollUp();
// break;
// aksi untuk tombol ke bawah (DOWN_ARROW)-----------------------------------------------
// case DOWN:
// aksinya di sini
// if (index >= 6) {
// index = index - 6;
// } else {
// index = index + 3;
// }
// break;
// aksi untuk tombol ke kiri (LEFT_ARROW)------------------------------------------------
case LEFT:
// aksinya di sini
if (index == 0) {
index = 8;
} else {
index = index - 1;
}
break;
// aksi untuk tombol ke kanan (RIGHT_ARROW)----------------------------------------------
case RIGHT:
// aksinya di sini
if (index == 8) {
index = 0;
} else {
index = index + 1;
}
break;
default:
break;
}
repaint();
}
// aksi untuk tombol right selection (RIGHT_SOFTKEY)-----------------------------------------
if (keyCode == -6) {
// aksinya di sini
// midlet.exitMIDlet(); // keluar dari MIDlet
}
// aksi untuk tombol left selection (LEFT_SOFTKEY)-------------------------------------------
if (keyCode == -5)
{
// aksinya di sini
}
repaint();
}