what do you mean by 'simply'? in addition to KEY_DOWN? do i need variable to keep track of what key is held down? why isn't this basic function part of the main library?
@hgy29, this would be good also because last time you added modifiers only to mouse events and not to key events, so to know if it is ctrl-key or alt-ctr-key etc such an isKeyPressed function would come handy (meanwhile i keep i do this by keeping track of key_down/key_up events for ctrl, alt, shift, etc.).
KEY_UP is only part of the solution. You need to keep track of which keys are held down, and register EVENT_FRAME so that your callback can be continuously applied.
Essentially KEY_UP and KEY_DOWN are only useful to set and unset the variables, while EVENT_FRAME will update the game state based on the keys being held. For that matter, better have this key handling done globally, then EVENT_FRAME can delegate and specify the sprites that need to updated.
@Amuseum, you do not need EVENT_FRAME to keep a table of values that is always uptodate with the information whether a key is pressed or not. just KEY_DOWN/KEY_UP is enough.
of course if you update your sprites according to the state of the key, then this may be done in EVENT_FRAME but that depends on your game. surely if you want something done at each frame you need something like that, that's independent of key handling in my opinion.
I won't say the KEY_UP and KEY_DOWN are only useful to record key states, because you often need to know how many times a specific key has been pressed/released, or even just if it has been pressed (and maybe released just after) recently.
Anyhow if one just need to know if a key is currently pressed (ex: left/right in a platformer game) to update the player position in an enter frame event, then yes it would be easier to just query current key state from gideros than having to listen for key presses and releases, as @Amuseum says.
I was intrigued by this so this morning I made a small class to manage key presses. You can register up and down events for individual keys and you can query their up/down state whenever you like too. Maybe it will work for you @Amuseum
Keys = Core.class()function Keys:init()
self.keys ={}
stage:addEventListener(Event.KEY_DOWN, function(e)-- key down listenerlocal keys = self.keys
local k = keys[e.keyCode]if k then
k.state =true-- set state downif k.onDown then k.onDown()end-- run callbackelse
keys[e.Keycode]={state =true}-- create if it does not existendend)
stage:addEventListener(Event.KEY_UP, function(e)-- key up listenerlocal keys = self.keys
local k = keys[e.keyCode]
k.state =false-- set state upif k.onUp then k.onUp()end-- run callbackend)endfunction Keys:registerDown(k, f)-- register key down callbacklocal keys = self.keys
if keys[k]then
keys[k].onDown = f -- set callbackelse
keys[k]={state =false, onDown = f}-- create and set callbackendendfunction Keys:registerUp(k, f)-- register key up callbacklocal keys = self.keys
if keys[k]then
keys[k].onUp = f -- set callbackelse
keys[k]={state =false, onUp = f}-- create and set callbackendendfunction Keys:state(k)-- return state of keylocal keys = self.keys
if keys[k]thenreturn keys[k].state -- return current stateelse
keys[k]={state =false}-- createendreturnfalseend
And an example of using it..
local keys = Keys.new()
keys:registerDown(KeyCode.E, function()print("e is down <img class="emoji" src="https://forum.giderosmobile.com/resources/emoji/frowning.png" title=":(" alt=":(" height="20" />")end)
keys:registerUp(KeyCode.E, function()print("e is up <img class="emoji" src="https://forum.giderosmobile.com/resources/emoji/smile.png" title=":)" alt=":)" height="20" />")end)localfunction onEnterFrame(e)if keys:state(KeyCode.A)thenprint("a held")endend
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
Comments
If it triggered, then that means that the key is not held down anymore
Likes: antix
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Likes: antix, talis
Likes: antix
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Essentially KEY_UP and KEY_DOWN are only useful to set and unset the variables, while EVENT_FRAME will update the game state based on the keys being held. For that matter, better have this key handling done globally, then EVENT_FRAME can delegate and specify the sprites that need to updated.
of course if you update your sprites according to the state of the key, then this may be done in EVENT_FRAME but that depends on your game. surely if you want something done at each frame you need something like that, that's independent of key handling in my opinion.
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Anyhow if one just need to know if a key is currently pressed (ex: left/right in a platformer game) to update the player position in an enter frame event, then yes it would be easier to just query current key state from gideros than having to listen for key presses and releases, as @Amuseum says.
Likes: keszegh
Likes: keszegh, Amuseum, hgy29, totebo, snooks