Skip to content

Instantly share code, notes, and snippets.

@mika76
Last active November 30, 2022 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mika76/8bb21dcc9209e53f97d0d13eb03d4adb to your computer and use it in GitHub Desktop.
Save mika76/8bb21dcc9209e53f97d0d13eb03d4adb to your computer and use it in GitHub Desktop.
pico-8 - handling button presses without repeat
--class for tracking button pressed
--adapted from https://www.lexaloffle.com/bbs/?tid=36866
button={
init=function()
btn_new={}
btn_press={}
for i=0,5 do
btn_new[i]=false
btn_press[i]=false
end
end,
update=function()
for i=0,5 do
if btn(i) then
btn_new[i]=not btn_press[i]
btn_press[i]=true
else
btn_new[i]=false
btn_press[i]=false
end
end
end,
pressed=function(b)
return btn_new[b]
end,
reset=function(b)
btn_new[b]=false
btn_press[b]=false
end
}
#include button.lua
function _init()
button.init()
end
function _update()
button.update()
--call button.reset() if you want to clear the array
--and wait for a new button to be pressed
if button.pressed(❎) then
print("pressed ❎")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment