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
@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.
@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.
@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.
@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
@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?
“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,”
@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
@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
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
@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%20Rican%20Frog.jpg',gotImage)
end
function gotImage(image,status,header)
img=image
end
@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.
@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%20Rican%20Frog.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