Howdy, Stranger!

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

Unexpected error

I copied and pasted one of the examples. I get an error on the line containing function setup(). Sometimes syntax error and sometimes ‘=‘ expected near function.
Why would this be?

Comments

  • dave1707dave1707 Mod
    Posts: 8,674

    Could you post more information. What example. Also a copy of several lines showing the code you got the error on.

  • I am trying to learn how to do a button. Hopefully I have included a photo showing the error.

    Other wise here is the code as copied and pasted. The error happens on the function setup() line.

    Ken

    go

    -- Simple Button

    -- Use this function to perform your initial setup
    function setup()

    end

    -- This function gets called once every frame
    function draw()
    -- This sets a dark background color
    background(40, 40, 50)
    -- This sets the line thickness
    strokeWi4dth(5)
    --draw the button (a rectangle)
    --WIDTH/2-100 is the x coordinate of the bottom left corner. This is the centre of the screen moved 100 pixels to the left
    --HEIGHT/2-50 is the y coordinate of the bottom left corner. This is the centre of the screen minus 50 pixels down
    --200 is the width of the rectangle
    --100 is the height of the rectangle
    rect(WIDTH/2-100,HEIGHT/2-50,200,100)
    end

    function touched(t)
    --this function is called every time the screen is touched
    --checks to see if the touch (represented by t) is inside the limits of the rectangle, if it is then play the sound
    if t.x>WIDTH/2-100 and t.x<WIDTH/2+100 and t.y>HEIGHT/2-50 and t.y<HEIGHT/2+50 then
    sound("A Hero's Quest:Attack Cast 1")

    end
    

    end

  • dave1707dave1707 Mod
    Posts: 8,674

    Here’s a working copy of your code.

    -- Simple Button
    
    function setup()
    end
    
    -- This function gets called once every frame
    function draw()
        -- This sets a dark background color
        background(40, 40, 50)
        -- This sets the line thickness
        strokeWidth(5)
        --draw the button (a rectangle)
        --WIDTH/2-100 is the x coordinate of the bottom left corner. This is the centre of the screen moved 100 pixels to the left
        --HEIGHT/2-50 is the y coordinate of the bottom left corner. This is the centre of the screen minus 50 pixels down
        --200 is the width of the rectangle
        --100 is the height of the rectangle
        rect(WIDTH/2-100,HEIGHT/2-50,200,100)
    end
    
    function touched(t)
        --this function is called every time the screen is touched
        --checks to see if the touch (represented by t) is inside the limits of the rectangle, if it is then play the sound
        if t.x>WIDTH/2-100 and t.x<WIDTH/2+100 and t.y>HEIGHT/2-50 and t.y<HEIGHT/2+50 then
            sound(asset.downloaded.A_Hero_s_Quest.Attack_Cast_1)        
        end
    end
    
  • Hi t,hanks that worked except for the sound file of course :
    sound(asset.downloaded.A_Hero_s_Quest.Attack_Cast_1)
    I will try and work out how to put a file in there.

    Also I discovered what was causing the error in the original script. For some reason line 1 contained the word go. I took that out and all good.

    Cheers

Sign In or Register to comment.