I'm not sure I correctly understand your question, so sorry if I reply way off.
To handle keystrokes in games, I'd use two threads:
- the UI thread catches the key strokes and forwards them to the rendering thread
- the rendering thread displays the animation at the correct timing. I'd also use a state variable in the rendering thread to handle the key states.
Something like this
In the main activity, so in the UI thread:
Using java Syntax Highlighting
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
{
view.queueEvent(new Runnable()
{
// This method will be called on the rendering
// thread:
public void run()
{
renderer.goLeft(true);
}});
return true;
}
return super.onKeyDown(keyCode, event);
}
}
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
{
view.queueEvent(new Runnable()
{
// This method will be called on the rendering
// thread:
public void run()
{
renderer.goLeft(false);
}});
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Parsed in 0.034 seconds, using
GeSHi 1.0.8.4
in the rendering thread:
Using java Syntax Highlighting
public class renderer
{
private boolean go_left = false;
public void goLeft(boolean value)
{
go_left = value;
}
public void render()
while(true)
{
// do timing
if (go_left)
{
// do left animation
}
}
Parsed in 0.030 seconds, using
GeSHi 1.0.8.4