Game Development Reference
In-Depth Information
spacebar. In this case, we would begin charging the missile on “just pressed,” in-
crease the power of the missile the longer the spacebar is “still pressed,” and fi-
nally launch the missile when “just released” occurs.
In order to support these four different results, we first must declare an
enum
of
possibilities:
enum
KeyState
StillReleased,
JustPressed,
JustReleased,
StillPressed
end
Then, when the input is updated, the current snapshot needs to be grabbed:
function
UpdateKeyboard()
// lastState and currentState are arrays defined
elsewhere,
// which store the entire state of the keyboard.
lastState
=
currentState
currentState
= get keyboard state
end
Finally, a function can be implemented that returns the correct
KeyState
value
depending on the permutation:
function
GetKeyState(
int
keyCode
)
if
lastState
[
keyCode
] ==
true
if
currentState
[
keyCode
] ==
true
return
StillPressed
else
return
JustReleased
end
else
if
currentState
[
keyCode
] ==
true
return
JustPressed