
Originally Posted by
shmoove
This tutorial takes you up to a stage where you are moving an image on screen in response to key presses. That should be easy to chance into incrementing counters.
shmoove
Thaks for the tutorial.
How can I get this KeyRepeated event work? If I do it like this KeyPressed works fine but if I keep key pressed nothing happens.
Code:
protected void paint( Graphics g )
{
//set the current color of the Graphics contect to the specified RRGGBB colour
g.setColor( 0xFFFFFF );
g.fillRect( 90, 90, 30, 30 );
g.drawLine(50+Startup.counter,50,50+Startup.counter,60);
g.setColor( colour );
g.fillRect( 50, 50, Startup.counter, 10 );
g.drawString(Integer.toString(Startup.counter),100,100,16|1);
}
/*
* called when a key is pressed and this Canvas is the
* current Displayable
*/
protected void keyPressed( int keyCode )
{
//get the game action from the passed keyCode
int gameAction = getGameAction( keyCode );
switch( gameAction )
{
case LEFT:
//set current colour to red
colour = 0xFF0000;
Startup.counter--;
break;
case RIGHT:
//set current colour to green
colour = 0x00FF00;
Startup.counter++;
break;
}
//schedule a repaint of the Canvas after each key press as
//currently do not have any main game loop to do this for us.
repaint();
}
/*
* key repeated
*/
protected void keyRepeated( int keyCode )
{
int gameAction = getGameAction( keyCode );
switch( gameAction )
{
case RIGHT:
Startup.counter++;
repaint();
break;
}
}
[EDIT]
Sorry, that code works fine. My emulator just doesn't understand it.
But now I have a new question. Is it possible to use numpad keys without using Canvas? I have tried but it says cannot find symbol: method getGameAction(int). I have imported javax.microedition.lcdui.*; but it won't help.