Yes. You need to keep track of when an arrow key "became pressed" in addition to its current state. Then the direction to choose is the one that "became pressed" more recently and is still currently pressed.
EDIT:
I've never written anything in FreeBasic and I made no attempt at making it look good but I got it working. I've followed your idea of "pressed time counters" instead of mine of "pressed timestamps". But I'm sure it could be done either way.
It needs work from somebody who actually knows better, but here it goes:
Somewhere in the global scope:
Code: Select all
DIM up_key_counter as integer
DIM down_key_counter as integer
DIM left_key_counter as integer
DIM right_key_counter as integer
DIM best_direction as integer
DIM best_counter as integer
Then somewhere inside the "master loop" before the original if/elseif block:
Code: Select all
IF carray(ccUp) > 0 THEN
up_key_counter += 1
ELSE
up_key_counter = 0
END IF
IF carray(ccDown) > 0 THEN
down_key_counter += 1
ELSE
down_key_counter = 0
END IF
IF carray(ccLeft) > 0 THEN
left_key_counter += 1
ELSE
left_key_counter = 0
END IF
IF carray(ccRight) > 0 THEN
right_key_counter += 1
ELSE
right_key_counter = 0
END IF
Next we'll be looking for the counter that has the lowest value but also greater than 0:
Code: Select all
best_direction = -1
best_counter = 32767
IF up_key_counter > 0 AND up_key_counter < best_counter THEN
best_direction = ccUp
best_counter = up_key_counter
END IF
IF down_key_counter > 0 AND down_key_counter < best_counter THEN
best_direction = ccDown
best_counter = down_key_counter
END IF
IF left_key_counter > 0 AND left_key_counter < best_counter THEN
best_direction = ccLeft
best_counter = left_key_counter
END IF
IF right_key_counter > 0 AND right_key_counter < best_counter THEN
best_direction = ccRight
best_counter = right_key_counter
END IF
And finally the new conditions for the original if/elseif block:
Code: Select all
ELSEIF best_direction = ccUp THEN
herow(0).ygo = 20
setdir = dirUp
ELSEIF best_direction = ccDown THEN
herow(0).ygo = -20
setdir = dirDown
ELSEIF best_direction = ccLeft THEN
herow(0).xgo = 20
setdir = dirLeft
ELSEIF best_direction = ccRight THEN
herow(0).xgo = -20
setdir = dirRight
I've tested it on Vikings a little bit and it seems to work fine.