I really tried tweaking this one, but I need your help. I was trying to figure out a way to make the player's sprite move less as you held down the keys. Specifically it was to try to make the sprite move only a certain amount when you held down a key, but stop until you released it and pressed it again, instead of doing it continuously as you held down the key (this would be for stuff like moving a character like on a grid, the way some old school CRPGs did).
I couldn't find a way to make it work. This is the code so far, that didn't accidentally stop the sprite from moving at all, or in two directions only (at least I figured out that THEN limits results from an IF statement to one, so I need ENDIF for more).
I couldn't find a way to make it work. This is the code so far, that didn't accidentally stop the sprite from moving at all, or in two directions only (at least I figured out that THEN limits results from an IF statement to one, so I need ENDIF for more).
Code (glbasic) Select
SETCURRENTDIR("Media") // go to media files
SETTRANSPARENCY RGB (244, 124, 0)
LIMITFPS 60
LOADSPRITE "whatever.bmp", 5 //any picture will do; the code here works with a 32x32 one
GLOBAL shield% = 5
GLOBAL spritex = 128
GLOBAL spritey = 128
GLOBAL spritespeed = 1
GLOBAL KeyHit = 0
WHILE KEY (01) = FALSE
MovePlayer()
DrawScreen()
WEND
FUNCTION MovePlayer:
LOCAL dirx, diry
IF KeyHit = 0
IF KEY (17)
diry = -1
KeyHit = 1
ENDIF
ELSE
diry = 0
KeyHit = 0
ENDIF
IF KeyHit = 0
IF KEY (30)
dirx = -1
KeyHit = 1
ENDIF
ELSE
dirx = 0
KeyHit = 0
ENDIF
IF KeyHit = 0
IF KEY (31)
diry = 1
KeyHit = 1
ENDIF
ELSE
diry = 0
KeyHit = 0
ENDIF
IF KeyHit = 0
IF KEY (32)
dirx = 1
KeyHit = 1
ENDIF
ELSE
dirx = 0
KeyHit = 0
ENDIF
spritex = spritex + (spritespeed * dirx)
spritey = spritey + (spritespeed * diry)
IF KEY (57) THEN spritespeed=3
IF NOT KEY (57) THEN spritespeed=1
IF spritex < 0 THEN spritex = 0
IF spritex > 608 THEN spritex = 608
IF spritey < 0 THEN spritey = 0
IF spritey > 448 THEN spritey = 448
ENDFUNCTION
FUNCTION DrawScreen:
DRAWSPRITE shield, spritex, spritey
SHOWSCREEN
ENDFUNCTION