Learning Defold Engine and making a small first game called: “Project0™”…
We have started designing the GUI interface for the above game.
Problem now is that a GUI button can be pressed and held down?
How would we make GUI button do one click like a normal button would do?
Relevant Lua source code is below, please someone suggest a fix in this forum thread, thank you.
Savant Savior
local globals = require "Global_Variables"
local consts = require "Constants"
local NORMAL_SCALE = vmath.vector3(1.0, 1.0, 1.0)
local PRESSED_SCALE = vmath.vector3(0.9, 0.9, 1.0)
local ANIM_DURATION = 0.15
function init(self)
msg.post(".", "acquire_input_focus")
gui.set_render_order(7)
self.btn_node = gui.get_node("GUI_Button_Exit")
end
function final(self)
end
function update(self, dt)
end
function on_message(self, message_id, message, sender)
end
function on_input(self, action_id, action)
if action_id == hash("touch") then
local button_node = gui.get_node("GUI_Button_Exit")
if action.pressed then
if gui.pick_node(button_node, action.x, action.y) then
self.button_pressed = true
gui.animate(button_node, "scale", PRESSED_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
end
elseif action.released and self.button_pressed then
if globals.Scene_Display_Timer > 1 then sound.play("#SFX_Button_Click") end
self.button_pressed = false
gui.animate(button_node, "scale", NORMAL_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
if gui.pick_node(button_node, action.x, action.y) then
globals.Scene_To_Display_Next = consts.Scene_Title
globals.Scene_Fade_Status = 1
globals.Scene_Fade_Alpha = 0.0
end
end
end
end
function on_reload(self)
end
Game Web HTML5 demo is below on itch Indie game distribution site:
One thing that might bite you is the button being triggered again while the fade is running, since on_input keeps firing. Try a self.transitioning flag that you set when the click is accepted and check at the top of on_input. Also return true from on_input after you handle the touch so other scripts with input focus do not react to the same click.
local globals = require "Global_Variables"
local consts = require "Constants"
local NORMAL_SCALE = vmath.vector3(1.0, 1.0, 1.0)
local PRESSED_SCALE = vmath.vector3(0.9, 0.9, 1.0)
local ANIM_DURATION = 0.1
function init(self)
msg.post(".", "acquire_input_focus")
gui.set_render_order(7)
globals.Button_Clicked = -1
globals.Button_Animation_Timer = -1
end
function final(self)
end
function update(self, dt)
if globals.Button_Animation_Timer > -1 then
globals.Button_Animation_Timer = globals.Button_Animation_Timer - 1
end
local button_node = gui.get_node("GUI_Button_Exit")
if globals.Button_Animation_Timer == 10 and globals.Button_Clicked == 1 then
gui.animate(button_node, "scale", NORMAL_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
end
if globals.Button_Animation_Timer == 0 and globals.Button_Clicked == 1 then
globals.Scene_To_Display_Next = consts.Scene_Title
globals.Scene_Fade_Status = 1
globals.Scene_Fade_Alpha = 0.0
sound.play("#SFX_Button_Click")
end
end
function on_message(self, message_id, message, sender)
end
function on_input(self, action_id, action)
if action_id == hash("touch") then
local button_node = gui.get_node("GUI_Button_Exit")
if action.pressed and globals.Button_Clicked == -1 then
if gui.pick_node(button_node, action.x, action.y) then
gui.animate(button_node, "scale", PRESSED_SCALE, gui.EASING_OUTBACK, ANIM_DURATION)
globals.Button_Animation_Timer = 20
globals.Button_Clicked = 1
end
end
end
end
function on_reload(self)
end
Use URL link above to download this WIP Defold Engine project (it is MIT open-source)!