Originally posted by Rishi Tyagi:
Hi Java gurus,
(...)
Actually when we work on games in j2me in that we have to move towards left,right,top and bottom and for that generally we trap the number keys 4,6,2 and 8 respectively why this?
why we can't trap the navigation keys here. and if we can trap these then whether we should these keys on the basis of keyCode parameter in keyPressed method of Canvas class or there is some other ways to do the same.
Hi Risji,
the Canvas class contains the method
getGameCode() for this purpose. Using this method, you can convert the code provided by
keyPressed() to one of the constants
Canvas.UP, Canvas.DOWN, Canvas.LEFT, Canvas.FIRE etc. The mapping performed by
getGameCode is device dependent, so on telephones without separate cursor keys, the corresponding numbers will be mapped to these constants, whereas on phones providing separate cursor keys these keys will be mapped to the game code constants...
The example canvas below shows how to control a little black square with the device cursor keys:
HTH,
Stefan