Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Making a button that when clicked goes to another page

edited August 2012 in Questions Posts: 2

Im new to programming in general. I have an idea for a game im making but im stuck on making a menu system. The concept sounds simple, but the code looks complicated. Can anyone recomend a tutorial or help piont me in the right direction?

Tagged:

Comments

  • Posts: 266

    here look at tutorials on @Reefwing's website. look for the one with finite state machines. that's what i use to make different screens/pages

  • dave1707dave1707 Mod
    Posts: 7,650

    @Batman_drinks_tea

    Here is a simple example using program states and a button to jump to different screens.


    function setup()     programState=firstScreen end function draw()     background(40, 40, 50)     programState()      end function touched(t)     if t.state==BEGAN then         if t.x>100 and t.x<300 and t.y>475 and t.y<525 then             if programState == firstScreen then                 programState=secondScreen                 return             end             if programState == secondScreen then                 programState=thirdScreen                 return             end              if programState == thirdScreen then                 programState=firstScreen                 return             end           end         end end function firstScreen()     fill(255)     fontSize(40)     text("First screen",200,600)     fontSize(20)     rectMode(CENTER)     rect(200,500,200,50)     fill(255,0,0)     text("Press here",200,500) end function secondScreen()     fill(255)     fontSize(40)     text("Second screen",200,600)     fontSize(20)     rectMode(CENTER)     rect(200,500,200,50)     fill(255,0,0)     text("Press here",200,500) end function thirdScreen()     fill(255)     fontSize(40)     text("Third screen",200,600)     fontSize(20)     rectMode(CENTER)     rect(200,500,200,50)     fill(255,0,0)     text("Press here",200,500) end
  • @ veeeralp
    Thanks man heres the link just in case anyone else needs this. It a very informative read. So are all of his tutorials actually.
    http://codeatuts.blogspot.com.au/search?updated-max=2012-07-12T06:03:00-07:00&max-results=7&start=6&by-date=false
    @ dave1707
    Cheers dude, this is exactly what i needed to get me started.

  • edited October 2012 Posts: 56

    Since the first day, I began programming, I was looking for such program, to allow the circulation between five sections (A,B,C,D,E) where the user is asked to choose from propositions on each section, and each time he chooses a proposition(A1,A2,A3,...), he gets a response in order to formulate at the end a solution for the management of a clinical situation.

  • Posts: 1,255

    @Letaief sounds like you're looking for a branching key. Usually, these break the choices down into two at a time (dichotomous) for simplicity's sake, but it is possible to build them with multiple choice. Everything from a choose your own adventure novel to a key for determining the species of fishes can be made this way.

    The original HyperCard can be thought of as an elaborate form of this type of application, and I wrote a extensible very for A+ version back in... well, ages ago.

    Shouldn't be a difficult challenge in Codea. We're you looking to build a specific key, or a program for building keys?

  • edited October 2012 Posts: 56

    Thanks for reply Mark, in fact i'm not looking for a branching key.

    Sections are dealing with clinical situations as follows

    Section A : Brief clinical presentation of a patient. with a variable amount of informations A1,A2,A3,A...
    Section B : Laboratory finding (B1,B2,B...)

    Section C : Imaging findings (C1,C2,C...)

    Section D : Therapeutic suggestions (D1,D2,D...)

    and each time a contributive proposition (A1..An,B1..Bn, ...) to solve the clinical situation is touched, it's answer is unhided (initial invisible answers on the screen) because in fact not all the propositions are needed and if so they have different grades (3 points, 2 points, 1 point, which are token in account for the final score).
    This particular point is important since the management of a clinical situation passes through differential diagnostics.

  • dave1707dave1707 Mod
    Posts: 7,650

    @letaief

    Still not sure what you're after, but here is another try. This displays 3 questions per page and depending on which question you tap, that answer shows. I don't know if you want to go from screen A to B to C to D all the time or if you want to go to different screens based on the answer. I have this set up to go to different screens based on different answers, but it's easily changeable. I don't know how you're going to load the questions and answers and how many there will be. Also, I don't know if when an answer is selected if you can change you mind and select another. This allows changing a selection.


    function setup()     displayMode(FULLSCREEN)     tab={}         currState=screen1 end function draw()     background(40, 40, 50)     textMode(CORNER)      currState()     text("NEXT PAGE",100,100)  end function touched(t)     if t.state==BEGAN then                  -- show next page         if t.x>50 and t.x<200 and t.y>50 and t.y<150 then             currState=nextState             ans=0         end                  -- questions / answers         select(t.x,t.y)     end end function select(x,y)     for z=1,#tab do         if x>tab[z].x and x<tab[z].y and y>tab[z].z and y<tab[z].a then             ans=z         end     end end function screen1()     fill(255)     text("A1 question",100,800)     tab[1]=vec4(100,200,800,850)         if ans==1 then         text("A1 answer goto B",200,750)         nextState=screen2     end          text("A2 question",100,700)     tab[2]=vec4(100,200,700,750)          if ans==2 then         text("A2 answer goto B",200,650)         nextState=screen2             end          text("A3 question",100,600)     tab[3]=vec4(100,200,600,650)          if ans==3 then         text("A3 answer goto C",200,550)         nextState=screen3            end     end function screen2()     fill(255)     text("B1 question",100,800)     tab[1]=vec4(100,200,800,850)         if ans==1 then         text("B1 answer goto C",200,750)         nextState=screen3            end          text("B2 question",100,700)     tab[2]=vec4(100,200,700,750)          if ans==2 then                 text("B2 answer goto A",200,650)         nextState=screen1     end          text("B3 question",100,600)     tab[3]=vec4(100,200,600,650)          if ans==3 then         text("B3 answer goto C",200,550)         nextState=screen3           end        end function screen3()     fill(255)     text("C1 question",100,800)     tab[1]=vec4(100,200,800,850)         if ans==1 then         text("C1 answer goto A",200,750)         nextState=screen1            end          text("C2 question",100,700)     tab[2]=vec4(100,200,700,750)          if ans==2 then         text("C2 answer goto B",200,650)         nextState=screen2           end          text("C3 question",100,600)     tab[3]=vec4(100,200,600,650)          if ans==3 then         text("C3 answer goto A",200,550)         nextState=screen1             end     end
  • dave1707dave1707 Mod
    Posts: 7,650

    @letaief

    Forget my code above, it's too complicated adding questions and answers. This is a lot easier and you don't have to set up code for different pages or calculations for the different touch areas. I still don't know exactly how you want to jump to the different pages based on the answers, or what happens after the last page, but maybe this will help somewhat. All you have to do is add more pages, questions, and answers to the qa() function.


    function setup()     displayMode(FULLSCREEN)     textMode(CORNER)         ans=0     sc=1     qa()    end function draw()     background(40, 40, 50)     fill(255)     showPage() end function showPage()     for z=1,#qtab[sc] do         str=string.format("%2d.) %s",z,qtab[sc][z])         text(str,100,900-z*80)         if ans==z then             text(atab[sc][ans],180,880-ans*80)         end     end     text("NEXT PAGE",300,100)     if ans==0 then         text("Make a selection",280,50)     end end function touched(t)     if t.state==BEGAN then                  -- next page selected         if t.y<120 then             if ans==0 then                 return             end             ans=0             sc = sc + 1             if sc>3 then                 sc=1             end         end                  -- questions selected         for z=1,#qtab[sc] do             if t.y>880-z*80 and t.y<920-z*80 then                 ans=z             end         end     end    end function qa()     -- create table of questions and answers     qtab={}     atab={}     for z=1,3 do    -- number of pages         qtab[z]={}         atab[z]={}         for y=1,7 do    -- max number of questions             qtab[z][y]=nil             atab[z][y]=nil         end     end           -- page1        qtab[1][1]="1-question number 1"     atab[1][1]="1-answer to question 1"       qtab[1][2]="1-question number 2"     atab[1][2]="1-answer to question 2"        qtab[1][3]="1-question number 3"     atab[1][3]="1-answer to question 3"        qtab[1][4]="1-question number 4"     atab[1][4]="1-answer to question 4"        qtab[1][5]="1-question number 5"     atab[1][5]="1-answer to question 5"      qtab[1][6]="1-question number 6"     atab[1][6]="1-answer to question 6"        qtab[1][7]="1-question number 7"     atab[1][7]="1-answer to question 7"               --page2      qtab[2][1]="2-question number 1"     atab[2][1]="2-answer to question 1"       qtab[2][2]="2-question number 2"     atab[2][2]="2-answer to question 2"        qtab[2][3]="2-question number 3"     atab[2][3]="2-answer to question 3"        qtab[2][4]="2-question number 4"     atab[2][4]="2-answer to question 4"        qtab[2][5]="2-question number 5"     atab[2][5]="2-answer to question 5"          -- page3     qtab[3][1]="3-question number 1"     atab[3][1]="3-answer to question 1"       qtab[3][2]="3-question number 2"     atab[3][2]="3-answer to question 2"        qtab[3][3]="3-question number 3"     atab[3][3]="3-answer to question 3"        qtab[3][4]="3-question number 4"     atab[3][4]="3-answer to question 4"        qtab[3][5]="3-question number 5"     atab[3][5]="3-answer to question 5"        qtab[3][6]="3-question number 6"     atab[3][6]="3-answer to question 6"                                                                                                                                    end
  • edited October 2012 Posts: 56

    Thanks to Dave1707 for the effort, I'll try to respond to the questions :

    First of all, the user can unhide any answer from any page, so a previous page button is needed.

    Second, each time an answer is unhided, it may be added to a summary page or just remain unhided on it's place, and the user jumps from page to page to get elements necessary for solving the clinical situation. and as mentioned in a previous post differential diagnostics may be a source of confusion and each diagnostic may be ruled out from the answers, added to that some questions and they're answers are givrent just to see if thé clinician or medical student is able to formulate and idea about the situation so he Never unhide unnecessary information.

    Third, from the code given, I Will create a function myQuestion With parmeters

    qtab,

    atab,

    weighttab with values ranging from 2 to 0, 2 contributive answer, 1 hall contributive, 0 unnecessary information

    checkedtab let us prepare the summary page
    ....

  • dave1707dave1707 Mod
    edited October 2012 Posts: 7,650

    don't know why there were multiple posts and why they were screwed up.

  • dave1707dave1707 Mod
    edited October 2012 Posts: 7,650

    @letaief
    I went thru all of you comments and modified my code based on what I think you wanted. I added vtab for the answer values, stab for summary answers, a summary page showing selected answers and score. Added next, previous, and summary page buttons. Toggle selected answers on/off in each section. Toggle the summary page on/off. One thing I wasn't sure of was, if there could be more than 1 selected answer per section. This code allows multiple answers per section. If you want only 1 answer per section, uncomment the line in the question section of the touched function. If this code is what you're after, then you can take it and modify the font, add color, etc to make it how you want.


    function setup()     supportedOrientations(PORTRAIT)     displayMode(FULLSCREEN)     maxPages=5     maxQuestions=10     page=1     summ=false     section={"Clinical","Laboratory","Imaging","Therapeutic","Section E"}     qa()   end function draw()     background(40, 40, 50)     fill(255)     showPage() end function showPage()     if summ then         showSummary()     else          textMode(CENTER)         text(section[page],WIDTH/2,1000)         textMode(CORNER)         for z=1,#qtab[page] do             str=string.format("%2d.) %s",z,qtab[page][z])             text(str,50,1000-z*80)             if stab[page][z] then                 text(atab[page][z],90,980-z*80)             end         end     end          textMode(CENTER)     rectMode(CENTER)     fill(35, 74, 67, 255)       rect(200,100,140,50)      rect(400,100,100,50)     rect(600,100,200,50)     fill(255)          if not summ then         if page>1 then             text("Previous page",200,100)         end         if page<maxPages then             text("Next page",400,100)         end     end         text("Summary page ( on/off )",600,100) end function touched(t)     if t.state==BEGAN then                  -- previous, next, summary         if t.y>75 and t.y<125 then             if t.x>130 and t.x<270 then    -- previous                 if not summ then                     page=page-1                     if page<1 then                         page=1                     end                 end             elseif t.x>350 and t.x<500 then     -- next                 if not summ then                     page=page+1                     if page>maxPages then                         page=maxPages                     end                 end             elseif t.x>500 and t.x<700 then    -- summary toggle                 summ = not summ                             end         else             -- question selection             for z=1,#qtab[page] do                 -- uncomment next line for 1 answer per section                 --stab[page][z]=false                 if t.y>980-z*80 and t.y<1020-z*80 then                     stab[page][z] = not stab[page][z]                 end                                end         end     end    end function showSummary()     line=0     score=0     textMode(CORNER)     for z=1,#stab do         for y=1,#stab[z] do             if stab[z][y] then                 line = line + 1                 text(atab[z][y],50,980-line*40)                 score=score+vtab[z][y]             end         end      end      textMode(CENTER)      str=string.format("Summary           Score  %3d",score)     text(str,WIDTH/2,1000) end function qa()     -- create tables     qtab={}    -- questions     atab={}    -- answers     stab={}    -- answers for summary page     vtab={}    -- score          for z=1,maxPages do         qtab[z]={}         atab[z]={}         stab[z]={}         vtab[z]={}         for y=1,maxQuestions do             qtab[z][y]=nil             atab[z][y]=nil             stab[z][y]=false             vtab[z][y]=0         end     end           -- page1        qtab[1][1]="1-question number 1"     atab[1][1]="1-answer to question 1"       vtab[1][1]=2     qtab[1][2]="1-question number 2"     atab[1][2]="1-answer to question 2"      vtab[1][2]=1         qtab[1][3]="1-question number 3"     atab[1][3]="1-answer to question 3"      vtab[1][3]=0         qtab[1][4]="1-question number 4"     atab[1][4]="1-answer to question 4"      vtab[1][4]=2         qtab[1][5]="1-question number 5"     atab[1][5]="1-answer to question 5"      vtab[1][5]=1        qtab[1][6]="1-question number 6"     atab[1][6]="1-answer to question 6"       vtab[1][6]=0         qtab[1][7]="1-question number 7"     atab[1][7]="1-answer to question 7"      vtab[1][7]=2                --page2      qtab[2][1]="2-question number 1"     atab[2][1]="2-answer to question 1"       vtab[2][1]=2        qtab[2][2]="2-question number 2"     atab[2][2]="2-answer to question 2"      vtab[2][2]=1           qtab[2][3]="2-question number 3"     atab[2][3]="2-answer to question 3"        vtab[2][3]=0         qtab[2][4]="2-question number 4"     atab[2][4]="2-answer to question 4"      vtab[2][4]=2           qtab[2][5]="2-question number 5"     atab[2][5]="2-answer to question 5"     vtab[2][5]=1       qtab[2][6]="2-question number 6"     atab[2][6]="2-answer to question 6"        vtab[2][6]=0         qtab[2][7]="2-question number 7"     atab[2][7]="2-answer to question 7"      vtab[2][7]=2           qtab[2][8]="2-question number 8"     atab[2][8]="2-answer to question 8"     vtab[2][8]=1      qtab[2][9]="2-question number 9"     atab[2][9]="2-answer to question 9"      vtab[2][9]=0          qtab[2][10]="2-question number 10"     atab[2][10]="2-answer to question 10"     vtab[2][10]=2                            -- page3     qtab[3][1]="3-question number 1"     atab[3][1]="3-answer to question 1"      vtab[3][1]=2        qtab[3][2]="3-question number 2"     atab[3][2]="3-answer to question 2"      vtab[3][2]=1         qtab[3][3]="3-question number 3"     atab[3][3]="3-answer to question 3"      vtab[3][3]=0          qtab[3][4]="3-question number 4"     atab[3][4]="3-answer to question 4"       vtab[3][4]=2          qtab[3][5]="3-question number 5"     atab[3][5]="3-answer to question 5"       vtab[3][5]=1        qtab[3][6]="3-question number 6"     atab[3][6]="3-answer to question 6"      vtab[3][6]=0           -- page4     qtab[4][1]="4-question number 1"     atab[4][1]="4-answer to question 1"      vtab[4][1]=2        qtab[4][2]="4-question number 2"     atab[4][2]="4-answer to question 2"      vtab[4][2]=1         qtab[4][3]="4-question number 3"     atab[4][3]="4-answer to question 3"      vtab[4][3]=0           -- page5     qtab[5][1]="5-question number 1"     atab[5][1]="5-answer to question 1"      vtab[5][1]=2        qtab[5][2]="5-question number 2"     atab[5][2]="5-answer to question 2"      vtab[5][2]=1         qtab[5][3]="5-question number 3"     atab[5][3]="5-answer to question 3"      vtab[5][3]=0       qtab[5][4]="5-question number 4"     atab[5][4]="5-answer to question 4"      vtab[5][4]=2                                                                                                                                                                                    end
  • edited October 2012 Posts: 56

    Thanks a lot, I was'nt expecting so much and I intended to continue on your previous code.
    One or more answers were expected by page.
    In response to your two previous questions

    1) I don't know how you're going to load the questions and answers and how many there will be.

    Answer 1)

    I am looking for the possibility of importing xml files, so the questions may be prepared with any other program under Windows ( Macromedia flash, Delphi, Java ), then exported to an xml file that will be used with this program allowing the use on multiple platforms, since xml may be combined to any program.

    2) Also, I don't know if when an answer is selected if you can change you mind and select another. This allows changing a selection

    Answer 2)

    If a selection is made the user is not allowed to change his opinion.

  • dave1707dave1707 Mod
    Posts: 7,650

    @letaief

    In the touched function under question selection, replace that code with this code. This allows only one answer per section, and once selected, won't allow you to change it. I'm not sure how many groups of questions you'll have, but one thing you can do is set up functions qa1(), qa2(), qa3(), etc, with a menu at startup to select which group of questions you want to show.


                -- question selection             for z=1,#qtab[page] do                 if stab[page][z] then                     return                 end             end             for z=1,#qtab[page] do                 if t.y>980-z*80 and t.y<1020-z*80 then                     stab[page][z] = true                 end                                end
  • edited October 2012 Posts: 56

    I added a check table for answers so they wont be changed and à table for thé names of sections


    -- DAVE1707 -- Use this function to perform your initial setup function setup()     supportedOrientations(PORTRAIT)     displayMode(FULLSCREEN)     maxPages=5     maxQuestions=10     page=1     summ=false     section={"Clinical","Laboratory","Imaging","Therapeutic","Section E"}     qa()   end function draw()     background(40, 40, 50)     fill(72, 213, 19, 255)     showPage() end function showPage()     if summ then         showSummary()     else          textMode(CENTER)         font("Georgia-Bold")         fontSize(40)         text(section[page],WIDTH/2,1000)         font("Georgia")         fontSize(20)         textMode(CORNER)         for z=1,#qtab[page] do             str=string.format("%s %2d) %s ",qsection[page],z,qtab[page][z])             text(str,50,1000-z*80)             if stab[page][z] then                 text(atab[page][z],90,980-z*80)             end         end     end          textMode(CENTER)     rectMode(CENTER)     fill(35, 74, 67, 255)       rect(200,100,140,50)      rect(400,100,100,50)     rect(600,100,200,50)     fill(255)          if not summ then         if page>1 then             text("Previous page",200,100)         end         if page<maxPages then             text("Next page",400,100)         end     end         text("Summary page ( on/off )",600,100) end function touched(t)     if t.state==BEGAN then                  -- previous, next, summary         if t.y>75 and t.y<125 then             if t.x>130 and t.x<270 then    -- previous                 if not summ then                     page=page-1                     -- PAGE=1 if we try to go under 1                     if page<1 then                         page=1                     end                 end             elseif t.x>350 and t.x<500 then     -- next                 if not summ then                     -- PAGE=maxPages if we try to go over maxPages                     page=page+1                     if page>maxPages then                         page=maxPages                     end                 end             elseif t.x>500 and t.x<700 then    -- summary toggle                 summ = not summ                             end         else             -- question selection             for z=1,#qtab[page] do                 -- uncomment next line for 1 answer per section                 --stab[page][z]=false                 if t.y>980-z*80 and t.y<1020-z*80 and checktab[page][z]==false then                     stab[page][z] = not stab[page][z]                     checktab[page][z] = not checktab[page][z]                 end                                end         end     end    end function showSummary()     line=0     score=0     textMode(CORNER)     for z=1,#stab do         for y=1,#stab[z] do             if stab[z][y] then                 line = line + 1                 text(atab[z][y],50,980-line*40)                 score=score+vtab[z][y]             end         end      end      textMode(CENTER)      str=string.format("Summary           Score  %3d",score)     text(str,WIDTH/2,1000) end function qa()     -- create tables     qtab={}    -- questions     atab={}    -- answers     stab={}    -- answers for summary page     vtab={}    -- score     qsection={} -- section     checktab={} -- yet checked answer          for z=1,maxPages do         qtab[z]={}         atab[z]={}         stab[z]={}         vtab[z]={}         checktab[z]={}         for y=1,maxQuestions do             qtab[z][y]=nil             atab[z][y]=nil             stab[z][y]=false             checktab[z][y]=false               vtab[z][y]=0         end              qsection[1]="C"     qsection[2]="L"                                        qsection[3]="I"     qsection[4]="T"     qsection[5]="E"                                                                                                                     end

    I called the program on my iPad Dave1707

  • dave1707dave1707 Mod
    Posts: 7,650

    Hi @letaief

    I see you're making code changes, color, font, tables, etc. That's great. I'm glad I was able to give you a foundation to work with and now it's up to you to add the changes that make the program do and look exactly how you want it to. If you get hung up on anything, just ask. Keep us posted.

  • edited October 2012 Posts: 56

    Thanks dave1707

    I have problems with the output of this program, I want to use the entire screen, but I am unable to use in a right manner the Slider class of the spritely demonstration program and get rid of iparameters to the Left.
    First let me explain what this code deals of, It's for my students for the explanation of CONTRAST in radiology on which is based standard radiology, we are unable to separate two adjascent structures unless they attenuate X RAYS differently and by playing around different values we CAN explain that perfect by showing how borders between different squares are hided or un hided.

    I will add the formula of CONTRAST for pedagogic considerations.

    Here is the code

    -- CONTRASTE
    -- Use this function to perform your initial setup
    function setup()
        iparameter("lr", 0, 255, 50)
        iparameter("rr", 0, 255, 50)
        iparameter("cr", 0, 255, 50)
        print("lr : Left Rectangle")
        print("rr : Right Rectangle")
        print("cr : Central Rectangle")
    end
    
    function carre(llx,lly,edge,type)
        if type==1 then
            fill(lr,lr,lr)
        end
        if type==2 then
            fill(rr,rr,rr)
        end
        if type==3 then
            fill(cr,cr,cr)
        end 
        rect(llx,lly,edge,edge)
    end
    
    -- This function gets called once every frame
    function draw()
        -- This sets a dark background color 
        background(230, 171, 92, 255)
        fill(255)
        background(14, 15, 14, 255)
        fill(0, 255, 16, 255)
        font("Futura-CondensedExtraBold")
        fontSize(28)
        textMode(CORNER)
        C=(lr-rr)/(lr+rr)
        text("Right-Left squares CONTRAST : " ..C, 10, HEIGHT - 50)  
        text("Value of lr : " ..lr,10, HEIGHT - 150)   
        text("Value of rr : " ..rr,380, HEIGHT - 150)   
        -- text("Right rectangle :"..rr, 10, HEIGHT - 80)
        carre(50,300,300,1)  
        carre(348,300,300,2)
        carre(150,400,100,3)
        carre(448,400,100,3)
        
        optimizedRect(300,25,50,lr,lr)
        optimizedRect(348,25,50,rr,rr)
        
        optimizedRect(50,25,50,lr,lr)
        optimizedRect(100,25,50,cr,cr)
        
        optimizedRect(550,25,50,rr,rr)
        optimizedRect(600,25,50,cr,cr)
        
    end
    
    
    function optimizedRect(llx,lly,width,height,rgb)
        fill(rgb,rgb,rgb)
        rect(llx,lly,width,height)
    end
    

    I Thank all the members of this forum and I hope we'll make Codea more and more Known and efficient in both fields Games and Pedagogy since Games may be programmed in order to explain à lot of things ( the coordinates program by Allomerus is the recent example to me ).

  • Jmv38Jmv38 Mod
    edited October 2012 Posts: 3,295

    Hello @letaief if you need sliders in fullscreen and very practical, i suggest you use the following:
    http://db.tt/Xh9yz7eD they don't occupy the screen surface, you dont have to be very accurate when you point them, etc... They were originally developped by Nat and i modified them somewhat.

Sign In or Register to comment.