![]() |
||
|
Puzzle 1 - Double Tap Running - Printable Version +- Little Fighter Empire - Forums (https://lf-empire.de/forum) +-- Forum: General Zone (https://lf-empire.de/forum/forumdisplay.php?fid=23) +--- Forum: Programming (https://lf-empire.de/forum/forumdisplay.php?fid=50) +--- Thread: Puzzle 1 - Double Tap Running (/showthread.php?tid=8839) |
||
Puzzle 1 - Double Tap Running - Azriel - 07-25-2013 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~ RE: Puzzle 1 - Double Tap Running - A-Man - 07-25-2013 Lol, what a coincidence. I just finished fixing that code for the engine I am writing (the A-Engine), and here is how I did it: In the main loop: http://pastebin.com/zLxzP6PK in the Update function of the object: http://pastebin.com/T7vCSaNA The interval between the hold and the release is calculated time-wise, such that if I ever decided to change the FPS, it won't effect the way it works. RE: Puzzle 1 - Double Tap Running - Azriel - 07-27-2013 your code reminds me of how I used to write code before I learnt to structure code nicely (you'll learn in time, though you'd learn fastest when you code in pairs with someone who structures code nicely). here's how I did mine:
Azriel~ RE: Puzzle 1 - Double Tap Running - A-Man - 07-27-2013 Wow! I liked how you functioned self-explanatory variable names in your code. It can all be understood just by going through the code. One thing I would like to ask is, why did you favor structs over classes? RE: Puzzle 1 - Double Tap Running - Azriel - 07-27-2013 I'm using a class there (I haven't used structs much, I'm actually not familiar with how structs work - haven't had a need to learn them). Azriel~ RE: Puzzle 1 - Double Tap Running - A-Man - 07-28-2013 Oh! Never mind then. I had this random assumption that the arrow sign was only used to call variables and functions under a struct. Thanks for clarification!. RE: Puzzle 1 - Double Tap Running - Som1Lse - 07-28-2013 (07-27-2013, 11:02 PM)Azriel Wrote: I'm using a class there (I haven't used structs much, I'm actually not familiar with how structs work - haven't had a need to learn them).Off-topic but: Structs are basically the exact same thing as classes in C++. The reason why they are there is because they were a part of C, and thus also in C++ to provide C compatibility. Everything you can do with classes you can do with structs as well, and compilers treat them as the same thing. There is one small difference though which is simply that by default members of a class are private while members of a struct are public. The only reason to use structs in C++ is to provide C compatibility. For example "sGame.h", which is used in both my data changer and the AI DLL, uses structs and C syntax, so it will work in both languages. |