button

How can I make a buttons that when I press each one it goes to a different scene?

Look at this link

http://twolivesleft.com/Codea/Talk/discussion/3519/how-can-you-have-multiple-screens#Item_18

I think it’s time we (someone) wrote this up for the wiki because it comes up so often!

thankyou @dave1707. how can i make it so i tap a button to go back to the main screen

@Andrewsimpson4 When you press a button to go to a different screen, then you would have a button on that screen to go back to the main screen when you tap it. You can have as many different screens with as many buttons as you want.

@Ignatz I think the main problem is it’s to hard to search the forum for what they’re looking for or they don’t know what to search for. I’m not sure what the new forum search shows because I have an iPad 1 and the search function doesn’t work for me.

how do you make it so it shows up on the function easy screen. I dont know where to put the code for the button @dave1707

I think i might have got it but I don’t know for sure because you can still click anywhere on the screen for it to work. How can i change it so you don’t have click anywhere on the screen?
@dave1707

@Andrewsimpson4 I was going to try to explain it, but its easier just to give you another example. What I did was put “Return to menu” on the Easy screen. You now have to tap on those words to return to the menu, but on the Normal and Hard screens, you can tap anywhere. I don’t like the way I originally wrote this, there are better ways of doing this.

EDIT: Several changes were made to the original code. See posts below for changes.


displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)        -- set for portrait mode

function setup()
    yy=0  -- changed for scroll
    func=menu
end

function draw()
    func()
end

function menu()
    background(0,0,200)  
    
    w=WIDTH/2            -- set width value
    eh=HEIGHT-400        -- set height of different options
    nh=HEIGHT-600        -- eh easy, nh normal, hh hard
    hh=HEIGHT-800
     
    fontSize(48)            -- set font size
    fill(255,0,0)            -- set font color
    
    menuTitle1="Main Menu"            -- main title
    menuTitle2="Select Option"    
    text(menuTitle1,w,HEIGHT-100)     -- display titles 
    text(menuTitle2,w,HEIGHT-200)      
 
    fill(0,255,0)           -- set easy values
    
    t1="Easy"
    text(t1,w,eh)
    w1,h1=textSize(t1)     -- width and height of easy text 
     
    t2="Normal"            -- set normal values
    text(t2,w,nh)
    w2,h2=textSize(t2)      -- width and height of normal text
    
    t3="Hard"                -- set hard values
    text(t3,w,hh)  
    w3,h3=textSize(t3)      -- width and height of hard text  
end

function easy()
    background(40,40,50)
    text("EASY screen",WIDTH/2,HEIGHT/2)
    t1="Return to menu"
    text(t1,w,100)
    w1,h1=textSize(t1)     -- width and height of easy text 
    t2="URL 1"
    text(t2,w,800)
    w2,h2=textSize(t2)     -- width and height of easy text 
    t3="URL 2"
    text(t3,w,700)
    w3,h3=textSize(t3)     -- width and height of easy text 
end

function normal()
    background(40,40,50)
    text("NORMAL screen",WIDTH/2,HEIGHT/2+yy)  -- changed for scroll
    t1="Return to menu"
    text(t1,w,100+yy)  -- changed for scroll
    w1,h1=textSize(t1)     -- width and height of easy text 
end

function hard()
    background(40,40,50)
    text("HARD screen",WIDTH/2,HEIGHT/2+yy)  -- changed for scroll
    t1="Return to menu"
    text(t1,w,100+yy)  -- changed for scroll
    w1,h1=textSize(t1)     -- width and height of easy text 
end

function url1()
    background(40,40,50)
    text("url 1 screen",WIDTH/2,HEIGHT/2) 
    if notOpen then 
        notOpen=false
        openURL('http://www.twolivesleft.com',true)
    end
    t1="Return to menu"
    text(t1,w,100)
    w1,h1=textSize(t1)     -- width and height of easy text 
end

function url2()
    background(40,40,50)
    text("url 2 screen",WIDTH/2,HEIGHT/2)
    if notOpen then 
        notOpen=false
        openURL('http://www.google.com',true)
    end
    t1="Return to menu"
    text(t1,w,100)
    w1,h1=textSize(t1)     -- width and height of easy text 
end

function touched(t)
    if t.state==BEGAN then
        if func==easy then
            if t.x>w-w1/2 and t.x<w+w1/2 and t.y>100-h1/2 and t.y<100+h1/2 then
                func=menu
            end
            if t.x>w-w2/2 and t.x<w+w2/2 and t.y>800-h2/2 and t.y<800+h2/2 then
                notOpen=true
                func=url1
            end
            if t.x>w-w3/2 and t.x<w+w3/2 and t.y>700-h2/2 and t.y<700+h2/2 then
                notOpen=true
                func=url2
            end
        end 
        if func==url1 or func==url2 then
            if t.x>w-w1/2 and t.x<w+w1/2 and t.y>100-h1/2 and t.y<100+h1/2 then
                func=menu
            end
        end
        if func==normal or func==hard then  -- changed for scroll
            if t.x>w-w1/2 and t.x<w+w1/2 and 
                    t.y>(100-h1/2+yy) and t.y<(100+h1/2+yy) then
                func=menu
                yy=0
            end
        end
        if func==menu then
            if t.x>w-w1/2 and t.x<w+w1/2 and t.y>eh-h1/2 and t.y<eh+h1/2 then
                func=easy
            end
            if t.x>w-w2/2 and t.x<w+w2/2 and t.y>nh-h2/2 and t.y<nh+h2/2 then
                func=normal
            end
            if t.x>w-w3/2 and t.x<w+w3/2 and t.y>hh-h3/2 and t.y<hh+h3/2 then
                func=hard
            end  
       end
    end  
    if t.state==MOVING then  -- changed for scroll
        yy=yy+t.deltaY
    end
end

thanks. but i need all of the different menu options to return. could you put that in the code them i think i will be good. @dave1707

@Andrewsimpson4 I made changes to the above code. Is that what you wanted.

yep perfect!!! thank you!!! @dave1707

one more thing i want to be able to open funtion easy and have a couple of url options to click on i coudnt figure out how to do this @dave1707

@Andrewsimpson4 I made changes to the above code. This code wasn’t supposed to be expanded upon with more screens and buttons. It’s getting messy. When I get time later, I’ll give you another version that can be expanded easier and not be messy.

ok but will it be able to open a URl as in on the internet. @dave1707

Sure just say openURL(“blah blah”, true)

lets say i have a word that says “open” and i only want it to open the page if i click open @JakAttak

@Andrewsimpson4 I made changes to the above code to open 2 different URL’s. When the URL is open, press done in the upper left corner to exit the URL and return back to the easy screen.

how can I make the hard and normal screen scrole up and down? @dave1707

@Andrewsimpson4 I added code to the above program to allow the normal and hard screens to scroll up or down. To see what changed, look for the variable yy.