So I wanted to make a game that was split into 2 tabs: TitleScreen and Game (You can probably guess what each one is). I initially wanted it so that if you pressed the start button in the titlescreen tab, the console would then run the Game() tab. For help, I looked at a codea discussion that basically showed how you could split your code into tabs. Forum discussion link: http://codea.io/talk/discussion/210/file-managementdistributing-code-over-tabs-answered/p1
This gave me a basic utility and Idea of how to set up such a system, but there was a part of it that I wanted to edit.
This is the part of the code from the discussion that needed editing (the main tab’s code):
-- Main
function setup()
scenes = {}
scenes[1] = Test1()
scenes[2] = Test2()
-- SceneIndex parameter from 1 to number of scenes
iparameter("SceneIndex",1,#scenes)
end
function draw()
scenes[SceneIndex]:draw()
end
function touched(touch)
scenes[SceneIndex]:touched(touch)
end
I wanted to change it so that I could have a global variable that you could change to change what tab the console is running instead of a parameter (so if x==1, then the console would run the TitleScreen tab. And if x==2, the console would run the game() tab). Unfortunately, as small as this code is, I could not entirely wrap my head around how the Scene[#] changes and also how the touched() function contributes to the code. Eventually I settled upon making a generic for loop that will only draw the scenes[] code if the table content selected == x (so that I could have the x=TitleScreen() and it would run the title screen tab or I could have x=Game() and it would run the game tab). As you would expect, this code is not working (for some reason).
Code for the Main tab:
function setup()
scenes = {}
scenes[1] = TitleScreen()
scenes[2] = Game()
x=TitleScreen()
-- SceneIndex parameter from 1 to number of scenes
end
for i,z in pairs(scenes) do
if scenes[z] == x then
function draw()
scenes[x]:draw()
end
end
end
Code for the TitleScreen tab (To show how me selecting “Start Game” should affect what tab the console runs):
TitleScreen = class()
function TitleScreen:init()
rectMode(CORNER)
spriteMode(CORNER)
textMode(CENTER)
st=false
cr=false
hs=false
end
function TitleScreen:draw()
sprite("SpaceCute:Background",0,0,WIDTH,HEIGHT)
stroke(0, 231, 255, 255)
strokeWidth(5)
fill(8, 255, 0, 255)
if st then
fill(11, 0, 255, 255)
end
rect(WIDTH/2-100,HEIGHT/2+125,200,50)
fill(8, 255, 0, 255)
if cr then
fill(11, 0, 255, 255)
end
rect(WIDTH/2-100,HEIGHT/2-25,200,50)
fill(8, 255, 0, 255)
if hs then
fill(11, 0, 255, 255)
end
rect(WIDTH/2-100,HEIGHT/2-175,200,50)
fill(255, 0, 22, 255)
text("Start Game",WIDTH/2,HEIGHT/2+150)
text("Credits",WIDTH/2,HEIGHT/2)
text("HighScores",WIDTH/2,HEIGHT/2-150)
textSize(150,30)
end
function touched(touch)
if touch.state == BEGAN then
if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and
touch.y >= HEIGHT/2+125 and touch.y <= HEIGHT/2+175 then
st=true
end
if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and
touch.y >= HEIGHT/2-25 and touch.y <= HEIGHT/2+25 then
cr=true
end
if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and
touch.y >= HEIGHT/2-175 and touch.y <= HEIGHT/2-125 then
hs=true
end
end
if touch.state==ENDED then
if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and
touch.y >= HEIGHT/2+125 and touch.y <= HEIGHT/2+175 then
x=Game()
end
if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and
touch.y >= HEIGHT/2-25 and touch.y <= HEIGHT/2+25 then
--credits Roll
end
if touch.x >= WIDTH/2-100 and touch.x <= WIDTH/2+100 and
touch.y >= HEIGHT/2-175 and touch.y <= HEIGHT/2-125 then
--Goes to Game Center HighScores
end
st=false
cr=false
hs=false
end
end
Please explain what I’m doing wrong(and what the proper syntax is), how the original forums Main tab works (in a full description), and any general tips that might improve my knowledge on this topic.
Thanks in advance.