I am trying to add keyboard control for my character in 2D platform game with JavaScript. The character can walk, dash and jump
. Here is my current code:
if(keyboard['LEFT']) {
//walk to left
//...
} else if (keyboard['RIGHT']) {
//walk to right
if(keyboard['SHIFT'] && !jump) {
//dash
}
if(keyboard['JUMP']) {
//jump to right
}
if(keyboard['SHIFT'] && keyboard['JUMP']) {
//dash and jump (move further and faster)
}
if(keyboard['JUMP'] && keyboard['SHIFT']) {
//hold the right key, pressed jump key, and then pressed shift key
//it expected to jump to right here, because character can not dash
//in midair, but it will conflict the if condition above.
}
} else if (keyboard['JUMP'] && onGround) {
//jump
} else if (keyboard['SHIFT']) {
//dash
if(keyboard['JUMP']) {
//dash and jump
}
}
I am confused that there are many if() else() statement in my code, and it seems that I need to control the order of keyboard inputs.Is there an easy way to handle that?Am I going the right way? If not, what is the common way to achieve that? Please, Any help would be appreciated.