Author Topic: Key Timers  (Read 2889 times)

0 Members and 1 Guest are viewing this topic.

Offline Compynerd255

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 336
  • Rating: +53/-4
  • Betafreak Games
    • View Profile
    • Betafreak Games
Key Timers
« on: March 15, 2011, 10:49:04 am »
I started this topic not because I have a question, but I have something cool to share. By default, in Axe, you can either use GetCSC to read your keys or read the input directly. However, this means that the key either responds once per press or responds as long as it is held down. What if you would like the key to instead respond every few frames, like a variable key repeat function? The solution is a key timer, which I will demonstrate below:

1. Set aside an area of free RAM, one byte per key you plan to read (the example uses 4 keys, one for each directional arrow):
Code: [Select]
0→L1
Fill(L1, 2

2. Create a subroutine that updates the key timers for each key:
Code: [Select]
{L1}+1*getKey(1)→{L1}
{L1+1}+1*getKey(2)→{L1+1}
{L1+2}+1*getKey(3)→{L1+2}
{L1+3}+1*getKey(4)→{L1+3}
Alternatively, instead of +1, you can add a variable that is updated by an interrupt.

3. After calling that subroutine, check for each key like so and preform the action:
Code: [Select]
If {L1}+1<127
...action...
End
...check the other keys...

4. After doing all you want to with the key, insert the following between your action and the End:
Code: [Select]
{L1}-[key delay in frames]→{L1}

The total effect of this code means that a key will register the instant it is pressed, and will then repeat in the delay you set.
This code is useful because not only does it let you control your key repeats, but it also condenses all your direct key reads to one place, allowing you to easily change the keys.
« Last Edit: March 15, 2011, 10:51:55 am by Compynerd255 »
The Slime: On Hold, preparing to add dynamic tiles

Axe Eitrix: DONE

Betafreak Games: Fun filled games for XBox and PC. Check it out at http://www.betafreak.com



Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Key Timers
« Reply #1 on: March 16, 2011, 11:27:52 pm »
That's actually pretty cool.  What happens if you do a delay for keys that already support multiple presses?

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Key Timers
« Reply #2 on: March 17, 2011, 12:09:03 am »
Pretty nite tutorial, thanks :)
« Last Edit: March 17, 2011, 12:09:13 am by juju2143 »

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline Compynerd255

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 336
  • Rating: +53/-4
  • Betafreak Games
    • View Profile
    • Betafreak Games
Re: Key Timers
« Reply #3 on: March 17, 2011, 10:28:02 am »
What happens if you do a delay for keys that already support multiple presses?
It doesn't matter what key you use, since you're reading the key directly instead of using GetCSC.

If you want an example of this code in action, read the source for Eitrix.
The Slime: On Hold, preparing to add dynamic tiles

Axe Eitrix: DONE

Betafreak Games: Fun filled games for XBox and PC. Check it out at http://www.betafreak.com



Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Key Timers
« Reply #4 on: March 17, 2011, 09:14:21 pm »
Hey, this looks pretty cool.  Great idea. ;D