Howdy, Stranger!

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

Function Question

in Questions Posts: 124

I was reading the function section of Codea for Beginners eBook - by Ignatz. Ignatz mention creating a function to know if a user has touched a button. In the example it only shows how to manually put in the users x.y. I want to know how to get the function to work while I’m touching the screen, which is doesn’t explain. Meaning if I’m not touching the sprite or button it prints false ,if I am it prints true.

Here’s the function example from the ebook

function setup()
    a = IsTouched({x=30,y=45,w=60,h=20},{x=62,y=81})
    print(a)
end

function draw()
    background(40, 40, 50)   
end

function IsTouched(b,u) --b items are for button, u for user 
    if u.x>=b.x and u.x<=b.x+b.w and u.y>=b.y and u.y<=b.y+b.h then 
    return true else return false 
    end 
end

Comments

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc You already have code in one of your programs to detect if a button is being touched. If it’s touched (BEGAN), set a variable with whatever text you want to display in draw. If the button isn’t being touched (ENDED), clear the variable or set to whatever you want.

  • Posts: 124

    @dave1707 I know but I was trying to learn functions more so I can start making my own. I was trying to see how the ebook function example was going to achieve “ know if a user has touched a button“. I thought I was missing out on some other way but guess not.

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc Button code is straight forward. If you touch the screen within the boundaries of the button, the button is touched and you do whatever code you want. If you lift you finger from a touched button, then you do whatever code for untouched.

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc Here's some code showing pressing and releasing a button.

    displayMode(STANDARD)
    
    function setup()
        button1={x=200,y=300,w=100,h=50}
        fill(255)
        rectMode(CENTER)
        msg=""
    end
    
    function draw()
        background(0)
        rect(button1.x,button1.y,button1.w,button1.h)
        text(msg,WIDTH/2,HEIGHT/2)
    end
    
    function touched(t)
        if t.state==BEGAN then
            if t.x>button1.x-button1.w/2 and t.x<button1.x+button1.w/2 and 
                    t.y>button1.y-button1.h/2 and t.y<button1.y+button1.h/2 then
                msg="button pressed"
                code="on"
            else
                msg="button missed"
                code="off"
            end
        end
        if t.state==ENDED and code=="on" then
            msg="button released"
            code="off"
        end
    end
    
  • Posts: 124

    @dave1707 Thanks. After reading the ebook more I realized what I was missing out on. It was callback functions, especially used with button touch and classes. I think I understand what they are used for but still learning to set up properly. In your words how do you explain callbacks and what do you use them for?

  • edited April 23 Posts: 616

    "codea calls certain functions whose names are "agreed upon" between codea and us programmers. draw is called many times per second, and our job is to update our drawn objects and redraw them. touched is called when the touch state changes, finger touches or lifts or moves. our job is to change what the program does after decoding what the fingers have done,"

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc It easier with a simple example. show() is a function that gets called anytime the value in the parameter changes. As you slide the parameter, you change value and show() get executed each time. So instead of having an if statement to execute show() anytime value changes, the callback gets executed. So basically it’s an easy way to execute some code once when something changes.

    function setup()
        parameter.integer("value",1,10,show)    
    end
    
    function show()
        output.clear()
        print(value)
    end
    
  • Posts: 124

    @RonJeffries @dave1707 Thank you for the explanations and the example code.

  • Posts: 124

    @dave1707 I didn’t want to make a new discussion for this so I’m just asking here.
    Why doesn’t this code run in Codea?

    repeat
        io.write("enter your guess : ")
        guess = io.read()
    until tonumber(guess) == 15
    
  • SimeonSimeon Admin Mod
    edited April 24 Posts: 5,364

    @Jarc the standard io Lua library isn't a part of Codea. You can implement something similar like this:

    function setup()
        parameter.text("Guess", "Enter Your Guess")
    end
    
    -- This function gets called once every frame
    function draw()
        -- This sets a dark background color 
        background(40, 40, 50)
    
        if tonumber(Guess) == 15 then
            text("Correct", WIDTH/2, HEIGHT/2)
        end
    end
    

    This sets up a UI text box in the side bar and stores whatever text is written in there into a global variable (Guess), this is then checked each frame by converting it into a number to see if it's equal to 15

  • Posts: 124

    @Simeon Ok, thanks!

  • Posts: 124

    @Simeon or @dave1707 one last question before I attempt. In this forum https://codea.io/talk/discussion/3436/on-screen-parameter someone converted a parameter slider on screen. Is this possible for the code above and any parameter?

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc Here’s a guess game example.

    displayMode(FULLSCREEN)
    
    function setup()
        bcolor= 0
        x=WIDTH/2
        y=HEIGHT/2+100
        w,h=200,50
        left=x-w/2
        right=x+w/2
        bottom=y-h/2
        top=y+h/2
        rectMode(CENTER) 
        str=""
        guess=math.random(1000)
        resp=""
    end
    
    function draw()
        background(30,30,0,255)
        if bcolor == 0 then
            fill(255, 0, 0, 255)
        else
            fill(128,0,0,255)
        end    
        rect(x,y,w,h)
        fill(255)
        strokeWidth(6)
        text("Tap button and enter guess  1 to 1000",x,y+200)   
        text(str,x,y)
        text(resp,WIDTH/2,y+100)       
    end
    
    function touched(t)
        if t.x > left and t.x < right and t.y > bottom and t.y < top then
            showKeyboard()
            bcolor=1
            str=""
        end
    end
    
    function keyboard(key)
        if key==RETURN then
            bcolor=0
            val=tonumber(str)
            if val<guess then
                resp="too low"
            elseif val>guess then
                resp="too high"
            else
                resp="that's correct"
                guess=math.random(1000)            
            end
        else
            str=str..key
        end
    end
    
  • edited April 24 Posts: 124

    @dave1707 That didn’t really answer my question, mainly it was about converting Parameters on screen but I found my answer. I actually need the answer for this next one though. How do I make whatever I type on screen get transferred to the http.request’s URL. That way I can have the user of the app paste a url and it uploads a picture. Not sure the significance of this code without that ability. No other Codea forums show how to do this, which is surprising.

    displayMode(FULLSCREEN)
    
    function setup()
        getImg()
    end
    
    function draw()
        background(40, 40, 50)
        if img==nil then
            fill(255)
            text("Loading image",WIDTH/2,HEIGHT/2)
        else
            sprite(img,WIDTH/2,HEIGHT/2,700)
        end
    end
    
    function getImg()
        http.request('https://dl.dropboxusercontent.com/s/3149mj9xjx1w71e/Costa Rican Frog.jpg',gotImage)
    end
    
    function gotImage(image,status,header)
        img=image
    end
    
  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc Just take what I show you for the guess game and use the input to get the http address. The guess game wasn’t just for guessing. It shows you how to use the screen and the keyboard function to get input from the user.

  • Posts: 124

    @dave1707 starting to think you’re messing with me but I went ahead and attempted. For starters your guessing game doesn’t show how to enable copy and paste, so that leaves me having to type the url address every time just to see my progress. Second, I already know how to get a basic user input on screen and how to use the keyboard, plenty of tutorials from Codea forums show that. I asked how I can get that user input to be linked to the http.request’s URL so when I copy and paste a URL for a picture it loads the picture on screen.

    displayMode(FULLSCREEN)
    
    function setup()
        getImg()
        bcolor= 0
        x=WIDTH/2
        y=HEIGHT/2+100
        w,h=200,50
        left=x-w/2
        right=x+w/2
        bottom=y-h/2
        top=y+h/2
        rectMode(CENTER) 
        str=""
    end
    
    function draw()
        background(40, 40, 50)
        if img==nil then
            fill(255)
            text("Loading image",WIDTH/2,HEIGHT/2)
        else
            sprite(img,WIDTH/2,HEIGHT/2,700)
        end
    
           if bcolor == 0 then
            fill(255, 0, 0, 255)
        else
            fill(128,0,0,255)
        end    
        rect(x,y,w,h)
        fill(255)
        strokeWidth(6)
        text(str,x,y)
    
    end
    
    function getImg()
        --http.request('https://dl.dropboxusercontent.com/s/3149mj9xjx1w71e/Costa Rican Frog.jpg',gotImage)
        http.request((""),gotImage)
    end
    
    function gotImage(image,status,header)
        img=image
    end
    
    function touched(t)
        if t.x > left and t.x < right and t.y > bottom and t.y < top then
            showKeyboard()
            bcolor=1
            str=""
        end
    end
    
    function keyboard(key)
        if key==RETURN then
            bcolor=0
            val=tostring(str)
        else
            str=str..key
        end
    end
    
  • dave1707dave1707 Mod
    Posts: 8,398

    Remove getImg() in setup(). Replace your getImg with this.

    function getImg()
        http.request("https://"..str,gotImage)
    end
    
  • edited April 24 Posts: 124

    @dave1707 nvm about the copy and paste anyways it didn’t work.

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc What didn’t work, the copy and paste or the URL request.

  • Posts: 124

    @dave1707 the URL request aka the code didn’t load the picture. Did you test before you sent me that?

    displayMode(FULLSCREEN)
    
    function setup()
        bcolor= 0
        x=WIDTH/2
        y=HEIGHT/2+100
        w,h=200,50
        left=x-w/2
        right=x+w/2
        bottom=y-h/2
        top=y+h/2
        rectMode(CENTER) 
        str=""
    end
    
    function draw()
        background(40, 40, 50)
        if img==nil then
            fill(255)
            text("Loading image",WIDTH/2,HEIGHT/2)
        else
            sprite(img,WIDTH/2,HEIGHT/2,700)
        end
    
           if bcolor == 0 then
            fill(255, 0, 0, 255)
        else
            fill(128,0,0,255)
        end    
        rect(x,y,w,h)
        fill(255)
        strokeWidth(6)
        text(str,x,y)
    
    end
    
    function getImg()
        http.request("https://"..str,gotImage)
    end
    
    function gotImage(image,status,header)
        img=image
    end
    
    function touched(t)
        if t.x > left and t.x < right and t.y > bottom and t.y < top then
            showKeyboard()
            bcolor=1
            str=""
        end
    end
    
    function keyboard(key)
        if key==RETURN then
            bcolor=0
            val=tostring(str)
        else
            str=str..key
        end
    end
    
  • Posts: 124

    @dave1707 ok I’m at the realization you were messing with me, yet another rabbit hole.

  • dave1707dave1707 Mod
    edited April 25 Posts: 8,398

    @Jarc Add getImg() to the keyboard function when RETURN is pressed.

    PS. You don’t need the val=tostring(str). str is already a string.

    function keyboard(key)
        if key==RETURN then
            bcolor=0
            val=tostring(str)
            getImg()
        else
            str=str..key
        end
    end
    
  • SimeonSimeon Admin Mod
    Posts: 5,364

    @Jarc the parameters in the sidebar can't be rendered outside of the sidebar unless you handle all the drawing and interaction yourself, using the sidebar allows you to add parameters with much less code than writing custom ones from scratch

  • Posts: 124

    @dave1707 Thank you very much. This is a continuation of the uploading image but with a added feature. How do I get this line

     img3=str
    

    To read as that and this

    img3=readImage()
    

    So basically I need the spritesheet url to read as readImage() so when I press extract it will work. May seem like a useless question to you but for a project I’m working on it will be useful for me. Here is the full code and a link for the spritesheet url to copy and paste in.

    Link www.kindpng.com/picc/m/12-129142_exemplo-de-spritesheet-sprite-sheet-megaman-png-transparent.png

    function setup()
        bcolor= 0
        x2=WIDTH/2
        y2=HEIGHT/2+100
        w2,h2=200,50
        left=x2-w2/2
        right=x2+w2/2
        bottom=y2-h2/2
        top=y2+h2/2
        rectMode(CENTER) 
        str=""
    
        ---extract
        tab2={}
        delay=0
        i=1
        img3=str
        iw=img3.width
        ih=img3.height
        dx,dy=0,HEIGHT/2-300
        sizeX=iw
        sizeY=ih    
        spriteMode(CORNER)
        parameter.text("wide")
        parameter.text("high")
        parameter.action("extract",extract)
        parameter.integer("scale",1,500,ih)
        img3=image(iw,ih)    
        setContext(img3)
        background(255)
        sprite(img3,0,0)
        setContext()    
        w=img3.width
        h=img3.height
    
    end
    
    function draw()
        background(40, 40, 50)
        if img3==nil then
            fill(255)
            text("Loading image",WIDTH/2,HEIGHT/2)
        else
            sprite(img3,WIDTH/2,HEIGHT/2,700)
        end
    
        if bcolor == 0 then
            fill(255, 0, 0, 255)
        else
            fill(128,0,0,255)
        end    
        rect(x2,y2,w2,h2)
        fill(255)
        strokeWidth(6)
        text(str,x2,y2)
    
        ---extract
        delay=delay+1    
        if #tab2>0 then
            if delay>10 then
                delay=1
                i=i+1
            end
            if i>#tab2 then
                i=1
            end
            sprite(tab2[i],dx,dy,scale)
        else
            sprite(img3,dx,dy,400)
        end
    end
    
    function getImg()
        http.request("https://"..str,gotImage)
    
    end
    
    function gotImage(image,status,header)
        img3=image
    end
    
    function touched(t)
        if t.x > left and t.x < right and t.y > bottom and t.y < top then
            showKeyboard()
            bcolor=1
            str=""
        end
    end
    
    function keyboard(key)
        if key==RETURN then
            bcolor=0
            val=tostring(str)
            getImg()
        else
            str=str..key
        end
    end
    
    function extract()
        tab2={}
        dx,dy=WIDTH/2,HEIGHT/2
        if wide=="" or high=="" then
            return
        end
        stepX=iw//wide
        stepY=ih//high
        for y=1,sizeY,stepY do
            for x=1,sizeX,stepX do
                table.insert(tab2,img3:copy(x,y,stepX,stepY))
            end
        end
    end
    

    Thanks again! I know I always swarm you with questions.

  • dave1707dave1707 Mod
    Posts: 8,398

    @Jarc Here’s the working code. You can go thru it and remove what’s not needed. To run this, just tap the red button then press enter without keying anything. I set the URL in setup to the variable aa. Then in the keyboard function when you press RETURN, I move aa to str. That way I didn’t have to key anything to get it working. You can remove those when you want to key a URL.

    function setup()
        aa="www.kindpng.com/picc/m/12-129142_exemplo-de-spritesheet-sprite-sheet-megaman-png-transparent.png"
        bcolor= 0
        x2=WIDTH/2
        y2=HEIGHT/2+100
        w2,h2=200,50
        left=x2-w2/2
        right=x2+w2/2
        bottom=y2-h2/2
        top=y2+h2/2
        rectMode(CENTER) 
        str=""
    
        ---extract
        tab2={}
        delay=0
        i=1
        img3=str
        iw=img3.width
        ih=img3.height
        dx,dy=0,HEIGHT/2-300
        sizeX=iw
        sizeY=ih 
        print(iw,ih)   
        spriteMode(CORNER)
        parameter.text("wide")
        parameter.text("high")
        parameter.action("extract",extract)
        parameter.integer("scale",1,500,200)
        img3=image(iw,ih)    
        setContext(img3)
        background(255)
        sprite(img3,0,0)
        setContext()    
        w=img3.width
        h=img3.height
    
    end
    
    function draw()
        background(40, 40, 50)
        if img3==nil then
            fill(255)
            text("Loading image",WIDTH/2,HEIGHT/2)
        end
    
        if bcolor == 0 then
            fill(255, 0, 0, 255)
        else
            fill(128,0,0,255)
        end    
        rect(x2,y2,w2,h2)
        fill(255)
        strokeWidth(6)
        text(str,x2,y2)
    
        ---extract
    
        if #tab2>0 then
            delay=delay+1    
            if delay>10 then
                delay=1
                i=i+1
            end
            if i>#tab2 then
                i=1
            end
            sprite(tab2[i],dx,dy,scale)
        else
            sprite(img3,dx,dy)
        end
    end
    
    function getImg()
        http.request("https://"..str,gotImage)
    end
    
    function gotImage(image,status,header)
        img3=image
        iw=img3.width
        ih=img3.height
    end
    
    function touched(t)
        if t.x > left and t.x < right and t.y > bottom and t.y < top then
            showKeyboard()
            bcolor=1
            str=""
        end
    end
    
    function keyboard(key)
        if key==RETURN then
            bcolor=0
            str=aa              -- remove this line
            getImg()
        else
            str=str..key
        end
    end
    
    function extract()
        tab2={}
        dx,dy=WIDTH/2,HEIGHT/2
        if wide=="" or high=="" then
            return
        end
        sizeX=iw
        sizeY=ih
        stepX=iw//wide
        stepY=ih//high
        for y=1,sizeY,stepY do
            for x=1,sizeX,stepX do
                table.insert(tab2,img3:copy(x,y,stepX,stepY))
            end
        end
    end
    
  • Posts: 124

    @dave1707 You're the man! Thanks! I see now why mine wasn’t working, I was so close yet so far.

Sign In or Register to comment.