Skip to content

Instantly share code, notes, and snippets.

@szilardhuber
Last active May 25, 2020 12:45
Show Gist options
  • Save szilardhuber/038972e6bc4881d882df7e8d5e9627d2 to your computer and use it in GitHub Desktop.
Save szilardhuber/038972e6bc4881d882df7e8d5e9627d2 to your computer and use it in GitHub Desktop.
Defold - menu and loader
local score = 0
local function load_menu(score_in)
msg.post("loader#mainmenu", "load")
if not (score_in == nil) then
score = score_in
end
end
local function unload_menu()
msg.post("loader#mainmenu", "unload")
end
local function load_game()
msg.post("loader#level", "load")
end
local function unload_game()
msg.post("loader#level", "unload")
end
function init(self)
msg.post(".", "acquire_input_focus")
load_menu()
end
function on_message(self, message_id, message, sender)
if message_id == hash("start_game") then
unload_menu()
load_game()
elseif message_id == hash("game_over") then
unload_game()
load_menu(message.score)
elseif message_id == hash("proxy_loaded") then
msg.post(sender, "enable")
msg.post("mainmenu:/go", "score", { score = score })
end
end
function init(self)
msg.post(".", "acquire_input_focus")
end
function on_message(self, message_id, message, sender)
if message_id == hash("score") then
local scorenode = gui.get_node("score")
gui.set_text(scorenode, "SCORE: " .. message.score)
end
end
function on_input(self, action_id, action)
if (action_id == hash("click") and action.released == true) then
local textStart = gui.get_node("start")
local textExit = gui.get_node("exit")
if (gui.pick_node(textStart, action.x, action.y)) then
msg.post("loader:/loader#loader", "start_game")
elseif (gui.pick_node(textExit, action.x, action.y)) then
msg.post("@system:", "exit", {code = 0})
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment