Howdy, Stranger!

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

Trying to make a title screen

edited October 2014 in Bugs Posts: 63

This is code for a title screen that seems to be bugged in the spot where I have the program constantly testing for if the player touched the "start game" icon. If you are touching the icon, I have it repeating code that changes the color of the icon until you take your finger off of it. I do this using a repeat until() loop. I have the 'until' set to (touch.status == ENDED) so that the code stops repeating once you are not touching the screen, but when I try to run this code, it gives me an error saying that I tried to index the global variable: touch, a nil value. Can someone please tell me how to fix this? Heres the code:


function setup() rectMode(CORNER) spriteMode(CORNER) textMode(CENTER) end function draw() sprite("SpaceCute:Background",0,0,WIDTH,HEIGHT) rect(WIDTH/2-100,HEIGHT/2+125,200,50) rect(WIDTH/2-100,HEIGHT/2-25,200,50) rect(WIDTH/2-100,HEIGHT/2-175,200,50) fill(8, 255, 0, 255) stroke(0, 231, 255, 255) strokeWidth(5) pushStyle() 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) popStyle() if CurrentTouch.x >= WIDTH/2-100 and CurrentTouch.x <= WIDTH/2+100 and CurrentTouch.y >= HEIGHT/2+125 and CurrentTouch.y <= HEIGHT/2+175 then repeat pushStyle() fill(255, 255, 255, 255) stroke(0, 36, 255, 255) strokeWidth(5) rect(WIDTH/2-100,HEIGHT/2+125,200,50) popStyle() pushStyle() text("Start Game",WIDTH/2,HEIGHT/2+150) fill(255, 0, 20, 255) textSize(150,30) popStyle() until(touch.state == ENDED) end end

Comments

  • Jmv38Jmv38 Mod
    Posts: 3,295

    you wrote

            until(touch.state == ENDED)
    

    but in this program you are using CurrentTouch, not touch.Replace accordingly.
    touch can be accessed only from within touched(touch) and you dont have this function defined.

  • edited October 2014 Posts: 63

    Though your explanation may be somewhat vague to someone who has not learned about the touched(touch) function, I revised my code using your advice yet it still does not work and I cannot figure out why. Here's the new code:

        function setup()
        rectMode(CORNER)
        spriteMode(CORNER)
        textMode(CENTER)
    end
    function draw()
    sprite("SpaceCute:Background",0,0,WIDTH,HEIGHT)
        rect(WIDTH/2-100,HEIGHT/2+125,200,50)
        rect(WIDTH/2-100,HEIGHT/2-25,200,50)
        rect(WIDTH/2-100,HEIGHT/2-175,200,50)
        fill(8, 255, 0, 255)
        stroke(0, 231, 255, 255)
        strokeWidth(5)
        pushStyle()
        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)
        popStyle()
    end
    function touch(touched)
        x=touch.x
        y=touch.y
        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
            repeat
            pushStyle()
            fill(255, 255, 255, 255)
            stroke(0, 36, 255, 255)
            strokeWidth(5)
            rect(WIDTH/2-100,HEIGHT/2+125,200,50)
            popStyle()
            pushStyle()
            text("Start Game",WIDTH/2,HEIGHT/2+150)
            fill(255, 0, 20, 255)
            textSize(150,30)
            popStyle()
            until(touch.state == ENDED)
            end
                    end
    end
    
  • dave1707dave1707 Mod
    Posts: 7,554

    @Paintcannon I modified your code a little. The function is touched(touch) not touch(touched). Also, you don't try to draw things in functions other than draw() or functions called from draw().


    function setup() rectMode(CORNER) spriteMode(CORNER) textMode(CENTER) st=false cr=false hs=false end function 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 st=false cr=false hs=false end end
  • Will do. Thanks for your support :)

Sign In or Register to comment.