I'm trying to make a 2d game using java microedition and i just want to make my control smoother but the problem is when i press the LEFT key while pressing the UP Key the condition is not working i dont know why
public void moveJet2() throws IOException{
int gameAction = getKeyStates();
if(gameAction==LEFT_PRESSED && gameAction==UP_PRESSED){
padX-=padXVel;
padY-=padYVel;
}
else if(gameAction==RIGHT_PRESSED){
padX += padXVel;
}
else if(gameAction==UP_PRESSED){
padY-=padYVel;
}
else if(gameAction==DOWN_PRESSED){
padY+=padYVel;
}
}
getKeyStates()returns the state of keys in a single int. The various keys have individual values. UP_PRESSED = 0x0002 and LEFT_PRESSED = 0x0004. So if you press UP on your d-pad while callinggetKeyStates(), you'll get 2 back, andif (getKeyStates()==UP_PRESSED)will thus be true. Likewise, if you press LEFT on your d-pad while callinggetKeyStates(), you'll get 4 back.But if you press UP and LEFT at the same time, you can't get back 2 and 4 - because that's obviously 2 ints - and
getKeyStates()only returns one int.What you do get back though, is rather simple: 2 + 4 = 6. So, asking
if (getKeyStates()==6)will be true if pressing UP and LEFT at the same time. Orif (getKeyStates()==UP_PRESSED+LEFT_PRESSED).Typically though, you would ask using bit-operators, like this:
Because using that approach will work with any of the 8 directions you can press at the same time.