Created
September 10, 2013 02:34
-
-
Save dermotbalson/6504325 to your computer and use it in GitHub Desktop.
template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# Notes | |
--[[ | |
This is a template for creating multi-tab projects | |
That is, projects where each tab can be run independently | |
This can be used for step by step tutorials, with users able to choose which tab runs | |
It can also be used for multi-stage projects, eg games where there are several stages like | |
* start menu | |
* play (maybe with multiple levels) | |
* win | |
* lose | |
Each of these can have their own tab, with its own draw function, touch function, etc | |
The way it works is that each tab (in this case, T1, T2, T3, T4) contains a table (called T1, T2, T3, T4). | |
That table contains the functions setup, draw, touched, and any other functions needed by that tab | |
If you don't understand how a table can contain functions, then it would help you to understand "pointers". These are covered in Lua for Beginners, here - https://www.dropbox.com/s/qh1e7ft4rvhlpt2/Lua%20for%20beginners.pdf | |
--]] | |
--# T1 | |
T1={} | |
function T1.setup() | |
print("T1") | |
end | |
function T1.draw() | |
background(50) | |
fontSize(48) | |
text("T1",400,400) | |
end | |
--# T2 | |
T2={} | |
function T2.setup() | |
print("T2") | |
end | |
function T2.draw() | |
background(50) | |
fontSize(48) | |
text("T2",400,400) | |
end | |
--# T3 | |
T3={} | |
function T3.setup() | |
print("T3") | |
end | |
function T3.draw() | |
background(50) | |
fontSize(48) | |
text("T3",400,400) | |
end | |
--# T4 | |
T4={} | |
function T4.setup() | |
print("T4 - touch the screen") | |
end | |
function T4.draw() | |
background(50) | |
fontSize(48) | |
text("T4",400,400) | |
end | |
function T4.touched(touch) | |
print("You touched",touch.x,touch.y) | |
end | |
--# Main | |
--Main | |
--This code manages which Code tab is run | |
--it remembers your last choice, and if you select a different one, it runs that instead | |
function setup() | |
tabs={T1,T2,T3,T4} | |
LastCode=readProjectData("Code") or 1 | |
parameter.integer("Choose_a_tab",1,#tabs,LastCode,RunCode) | |
parameter.action("Previous tab", RunPrevious) | |
parameter.action("Next tab",RunNext) | |
end | |
function RunCode() | |
output.clear() | |
saveProjectData("Code",Choose_a_tab) | |
print("CODE "..Choose_a_tab) | |
if c and c.cleanup then c.cleanup() end | |
c=tabs[Choose_a_tab] | |
c.setup() | |
end | |
function RunPrevious() | |
if Choose_a_tab>1 then | |
Choose_a_tab=Choose_a_tab-1 | |
RunCode() | |
end | |
end | |
function RunNext() | |
if Choose_a_tab<#tabs then | |
Choose_a_tab=Choose_a_tab+1 | |
RunCode() | |
end | |
end | |
function draw() | |
c.draw() | |
end | |
function collide(contact) | |
if c.collide then c.collide(contact) end | |
end | |
function touched(touch) | |
if c.touched then c.touched(touch) end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment