#19359 - mr_schmoe - Sat Apr 17, 2004 10:45 pm
Ok, here's the skinny. I want to detect when the player moves the d-pad in one of eight directions. Also, it needs to tell whether it is a new direction or the same one. In others words, if the player first presses the down button it issues a command to start the player moving down (that is, load the proper animation and set the action variable to moving in the down direction,) but then it shouldn't issue that same command next cycle around. I save the key register to compare it with the currect key register next cycle to detect that. Four directions isn't the issue, it's when I work with eight directions it get complicated. here's the code I'm using, please pardon the sloppyness of it.
Any suggestions? I could figure this out on my own, it might take a while, but currently I have to start getting ready for work. And besides it get others a chance to work those brain mussels. Thanks alot.
Code: |
static u16 lastkeys; u16 currkeys = 0; u16 command = 0; /* each of the four directions bit0: 0 - pressed, 1 - released bit1: 0 - same as last cycle, 1 - just barely i.e. 00 = 0 = the key was pressed in the previous cycle and in this cycle, contiously pressed 11 = 3 = the key was barely released */ u8 arrowkeys[4]; u16 action; currkeys = KEYS; // get the current keys from the register if ((~currkeys & KEY_UP) && (!(~lastkeys & KEY_UP))) arrowkeys[0] = 2; if ((!(~currkeys & KEY_UP)) && (~lastkeys & KEY_UP)) arrowkeys[0] = 3; if ((!(~currkeys & KEY_UP)) && (!(~lastkeys & KEY_UP))) arrowkeys[0] = 1; if ((~currkeys & KEY_UP) && (~lastkeys & KEY_UP)) arrowkeys[0] = 0; if ((~currkeys & KEY_DOWN) && (!(~lastkeys & KEY_DOWN))) arrowkeys[1] = 2; if ((!(~currkeys & KEY_DOWN)) && (~lastkeys & KEY_DOWN)) arrowkeys[1] = 3; if ((!(~currkeys & KEY_DOWN)) && (!(~lastkeys & KEY_DOWN))) arrowkeys[1] = 1; if ((~currkeys & KEY_DOWN) && (~lastkeys & KEY_DOWN)) arrowkeys[1] = 0; if ((~currkeys & KEY_LEFT) && (!(~lastkeys & KEY_LEFT))) arrowkeys[2] = 2; if ((!(~currkeys & KEY_LEFT)) && (~lastkeys & KEY_LEFT)) arrowkeys[2] = 3; if ((!(~currkeys & KEY_LEFT)) && (!(~lastkeys & KEY_LEFT))) arrowkeys[2] = 1; if ((~currkeys & KEY_LEFT) && (~lastkeys & KEY_LEFT)) arrowkeys[2] = 0; if ((~currkeys & KEY_RIGHT) && (!(~lastkeys & KEY_RIGHT))) arrowkeys[3] = 2; if ((!(~currkeys & KEY_RIGHT)) && (~lastkeys & KEY_RIGHT)) arrowkeys[3] = 3; if ((!(~currkeys & KEY_RIGHT)) && (!(~lastkeys & KEY_RIGHT))) arrowkeys[3] = 1; if ((~currkeys & KEY_RIGHT) && (~lastkeys & KEY_RIGHT)) arrowkeys[3] = 0; if ((arrowkeys[0] == 2) && (arrowkeys[1] & 1) && (arrowkeys[2] & 1) && (arrowkeys[3] & 1)) command = WALK_BACK; if ((arrowkeys[1] == 2) && (arrowkeys[0] & 1) && (arrowkeys[2] & 1) && (arrowkeys[3] & 1)) command = WALK_FORWARD; if ((arrowkeys[2] == 2) && (arrowkeys[0] & 1) && (arrowkeys[1] & 1) && (arrowkeys[3] & 1)) command = WALK_LEFT; if ((arrowkeys[3] == 2) && (arrowkeys[0] & 1) && (arrowkeys[1] & 1) && (arrowkeys[2] & 1)) command = WALK_RIGHT; if ((~arrowkeys[2] & 1) && (~arrowkeys[1] & 1) && (arrowkeys[0] & 1) && (arrowkeys[3] & 1)) command = WALK_FORWARD_LEFT; if ((~arrowkeys[3] & 1) && (~arrowkeys[1] & 1) && (arrowkeys[0] & 1) && (arrowkeys[2] & 1)) command = WALK_FORWARD_RIGHT; if ((~arrowkeys[2] & 1) && (~arrowkeys[0] & 1) && (arrowkeys[1] & 1) && (arrowkeys[3] & 1)) command = WALK_BACK_LEFT; if ((~arrowkeys[3] & 1) && (~arrowkeys[0] & 1) && (arrowkeys[1] & 1) && (arrowkeys[2] & 1)) command = WALK_BACK_RIGHT; if (command == WALK_BACK) action = 8; if (command == WALK_FORWARD) action = 9; if (command == WALK_LEFT) action = 10; if (command == WALK_RIGHT) action = 11; if (command == WALK_BACK_LEFT) action = 12; if (command == WALK_BACK_RIGHT) action = 13; if (command == WALK_FORWARD_LEFT) action = 14; if (command == WALK_FORWARD_RIGHT) action = 15; if (action == 8) // do some appropreate action stuff ... |
Any suggestions? I could figure this out on my own, it might take a while, but currently I have to start getting ready for work. And besides it get others a chance to work those brain mussels. Thanks alot.