Friday, September 2, 2022
HomeGame DevelopmentTips on how to deal with keyboard enter arrow keys for a...

Tips on how to deal with keyboard enter arrow keys for a 2.5D Platformer


The way in which I course of keys presses in my purposes is I’ve 2 lists starting from 0-127 (all ASCII keys). One is for a key_duration_state and the opposite for a key_hold_state. I do not know what kind of occasion listener you are utilizing, however take a generic onKeyDown/Up(int key) listener perform for instance. This features job is to let you understand when a key has been pressed, the place key was the worth of the important thing pressed. I’ve it return 1 so long as the bottom line is nonetheless pressed, and 0 when it is launched. All I would like with the intention to register each ASCII keys press/launch state is go my record to the perform. Similar goes for period. Simply increment that keys duration_state so long as its pressed. Then reset it to zero upon launch:

// tells if its pressed
bool onKeyDown(int key) {
    key_hold_state[key] = 1;
    key_duration_state[key]++;
}

// tells if its launched
bool onKeyUp(int key) {
    key_hold_state[key] = 0;
    key_duration_state[key] = 0;
}

What I do to find out if a key’s being held for a period is use my record key_hold_state which shops whether or not the bottom line is pressed down or not. Then I’ve one other perform, say isHeldFor(int period) which checks if that key was held for the period:

bool wasHeldFor1s = keyListener.isHeldFor('A', DURATION_1s);
bool isHeldFor(int key, int period) {
    return duration_held_state[key] >= period);
}

That is simply my strategy. There could possibly be a greater manner, however storing each keys states into lists is an effective transfer. Retains it clear, concise, and arranged.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments