A thing that detects when you tap or doubletap directions using the keyboard arrowkeys. It was a bit of a challenge to do with diagonals working. Not sure if my implementation is that efficient, but it seems to work OK.
Could be used for a lot of things. In this example, you get a very simple push the instant you do a doubletap.
Without much effort, you could change it so that upon double-tapping, you are put into a state (such as afterburner, or sprinting), which only ends once you finish pressing the direction.
Here's the code.
Rem Project: 8 way doubletap
Rem Created: 05/12/2006 02:25:21
Rem ***** Main Source File *****
`want to make a thing where can doubletap in 8 directions (including diagonals), using only
`4 keys- eg. WASD or arraow keys.
`this presents a difficulty in that it is not easy for a person to press and release two keys simultaneously
`first do a thing to recognise diagonal taps
`up and release (within some time) constitutes an up tap, -similar for d,l,r
`a transition from nothing to diagonal up/right to nothing again (within some time)
` , without pressing either left or down, constitutes a diagonal u/r diagonal tap
sync on:sync rate 60
backdrop on
`quarter second- in 60 fps frames=> 15
tapframes=15
doubleframes=30
`result of input i following last result j
`this could be boolean- it either selects the old result, or the new one
dim result(9,9)
`all void except
for i=1 to 9
result(i,i)=i
result(i,0)=0
result(i,5)=i
next i
result(5,0)=5
`diag u/l
result(8,7)=7
result(4,7)=7
result(7,8)=7
result(7,4)=7
`diag u/r
result(8,9)=9
result(6,9)=9
result(9,8)=9
result(9,6)=9
`diag d/l
result(2,1)=1
result(4,1)=1
result(1,2)=1
result(1,4)=1
`diag d/r
result(2,3)=3
result(6,3)=3
result(3,2)=3
result(3,6)=3
`array of direction for the keys
dim direction#(9,1)
diag#=sqrt(0.5)
direction#(1,0)=0-diag#
direction#(4,0)=-1.0
direction#(7,0)=0-diag#
direction#(2,0)=0
direction#(5,0)=0
direction#(8,0)=0
direction#(3,0)=diag#
direction#(6,0)=1.0
direction#(9,0)=diag#
direction#(1,1)=0-diag#
direction#(2,1)=-1.0
direction#(3,1)=0-diag#
direction#(4,1)=0
direction#(5,1)=0
direction#(6,1)=0
direction#(7,1)=diag#
direction#(8,1)=1.0
direction#(9,1)=diag#
boxsize=10
posx#=300
posy#=300
standardthrust#=0.2
boostthrust#=5.0
damp#=0.95
do
`calculate "input"- do like the numeric pad- eg 7 is up/left, etc.
dir8way=5+rightkey()-leftkey()+3*(upkey()-downkey())
`make void if opposite directions are pressed
if rightkey() and leftkey():dir8way=0:endif
if upkey() and downkey():dir8way=0:endif
text 0,0,str$(dir8way)
if dir8way=5
if countdown>0 and dir_result<>0 and dir_result<>5
text 100,0,str$(dir_result)
required_for_double=dir_result
double_countdown=doubleframes
endif
countdown=tapframes
else
dec countdown:if countdown<0 then countdown=0
endif
dir_result=result(dir8way,dir_result)
`not sure if here is best order
dec double_countdown:if double_countdown<0 then double_countdown=0
if dir_result=required_for_double and double_countdown>0
text 200,0,str$(required_for_double)
`give a speed boost!
velx#=velx#+boostthrust#*direction#(dir_result,0)
vely#=vely#+boostthrust#*direction#(dir_result,1)
`current input is voided- player will have to go through neutral
`to get a new first tap
dir_result=0
double_countdown=0
endif
text 0,10,str$(dir_result)
`affect speed of a thing
`standard movement
velx#=damp#*velx#+standardthrust#*direction#(dir8way,0)
vely#=damp#*vely#+standardthrust#*direction#(dir8way,1)
posx#=posx#+velx#
`negative so controls are right way up
posy#=posy#-vely#
box posx#,posy#,posx#+boxsize,posy#+boxsize
sync
loop
You'll be able to click on this someday.