07-25-2013, 07:56 AM
For all of you interested in data changing, here is a programming puzzle which I recently solved (again):
Write some code as part of the "update" function that provides double tap running behaviour for a character. It may be pseudo code, real code, or just something that is understandable.
Scenario:
To run, the user presses [left] or [right], and must release it within 10 frames of the initial keypress. After the key release, the user must press the same button again in order to run in that direction (so pressing [left] [right] doesn't make the character run right).
Info:
The update function is called every frame. You are to provide the logic that determines whether the character should go into the running sequence.
You have access to the "facingRight" variable of the character, and you can update this variable.
You have access to the left/right key states.
You can declare as many variables as you want, but it is possible to do this with just one more besides the facingRight and input variables.
Stub:
The code stub above is a highly stripped down version of what I've got, but just want to see how other people try to solve this problem. Initially I used two variables, but I managed to do it in just one.
p/s: you can ask me to clarify things, I wrote this post with a lot of contextual knowledge which you might need that I have left out.
Azriel~
Write some code as part of the "update" function that provides double tap running behaviour for a character. It may be pseudo code, real code, or just something that is understandable.
Scenario:
To run, the user presses [left] or [right], and must release it within 10 frames of the initial keypress. After the key release, the user must press the same button again in order to run in that direction (so pressing [left] [right] doesn't make the character run right).
Info:
The update function is called every frame. You are to provide the logic that determines whether the character should go into the running sequence.
You have access to the "facingRight" variable of the character, and you can update this variable.
You have access to the left/right key states.
You can declare as many variables as you want, but it is possible to do this with just one more besides the facingRight and input variables.
Stub:
C++-Code:
// called every time unit that the game runs void Character::update() { // implement logic here // variables you have access to are "facingRight", input[LEFT], and input[RIGHT] // you may update the facingRight variable // you can have persistent variables that outlast this update function // e.g. // if (input[RIGHT] == true) { // // should we run? // } } |
The code stub above is a highly stripped down version of what I've got, but just want to see how other people try to solve this problem. Initially I used two variables, but I managed to do it in just one.
p/s: you can ask me to clarify things, I wrote this post with a lot of contextual knowledge which you might need that I have left out.
Azriel~