public static int getGameAction(int keyCode); |
public int getGameAction(int keyCode) public int getKeyCode(int gameAction) |
//不好的例子,不要这么做: class TetrisCanvas extends Canvas { int leftKey, rightKey, downKey, rotateKey; void init (){ //FOLLOWING MUST NOT BE DONE leftKey = getKeyCode(LEFT); rightKey = getKeyCode(RIGHT); downKey = getKeyCode(DOWN); rotateKey = getKeyCode(FIRE); } public void keyPressed(int keyCode) { if (keyCode == leftKey) { moveBlockLeft(); } else if (keyCode = rightKey) { ... } } } |
class TetrisCanvas extends Canvas { void init (){ } public void keyPressed(int keyCode) { int action = getGameAction(keyCode); switch (action) { case Canvas.LEFT: moveBlockLeft(); break; case Canvas.RIGHT: moveBlockRight(); break; } } } |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |