It looks like you're new here. If you want to get involved, click one of these buttons!
Hi, I've written a code for make a button and pressed it but it doesn't work.
I don't know why '
Main :
-- Test
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
local x = 200
local y = 200
local h = 50
local w = 100
parameter("x", 50, 1000)
parameter("y", 50, 1000)
parameter("h", 5, 200)
parameter("w", 10, 400)
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
setbutton(x,y,w,h)
local push=push(x,y,h,w)
if push == 1 then
print("Button pressed !")
end
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
end
Roundrect :
function setbutton(x,y,h,w)
stroke(216, 119, 44, 255)
rect(x,y,h,w)
end
function push(x,y,h,w)
local tx
local ty
if touch.x== nil then
tx = 0
ty = 0
else
touch.x = tx
touch.y = ty
end
if tx>x and tx<x+w and ty>y and ty<y+h then
return 1
end
end
This code return this error :
error : error : [ string " function setbutton( x,y,h,w) ..."] attempt to index global 'touch' (a nil value)
Thanks for explain me this error.
Comments
I believe it is because you defined your x,y,h, and w as local variables, then tried to use them outside of the function where they were defined. Comment out the local lines.
When you touch the viewer, Codea itself tries to call a function named
touched(touch)
(if it exists). So your code needs to be something like the following:@eloi67 well done on trying to work this out from scratch yourself. If @jlslate and @mpilgrem suggestions haven't help you solve the issue then have a look at:
http://codeatuts.blogspot.com.au/2012/06/tutorial-3-simple-button-class.html
Thanks for all your answers . My code work now !
But when I tried to modify the function to modify colors when call the function, there is an other error :
Error: error: [ string "function setup() ..."] :42: attemp to index local 'button_color' ( a nil value )
Hi @ eloi67
Might not be the best solution but the following works
You need to define button_color as an array up front, so after he=950 add
And in the set button function change the references from [x] to x where x is the position in the table/array. E.g button_color[1] becomes button_color_1_
I'm not sure why square bracket referencing doesn't work but this reflects the variable name on the left hand pane with the adjusters.
Also you may wish to change the parameter to iparameter as the colour components will only be integers.
Finally the parameter values should run 0,255,y where y is your default value. You have it the other way round at the moment
One other code :
One other error : Error: error: [ string "function setup() ..."] :4: attemp to index local 'buttoncolorg' ( a nil value )
buttoncolorg = {}
(orbuttoncog = {}
- the error message and the code are a bit out of step on the name)You need to initialise tables before you can start assigning values to it.
This code :
Must print " great " when I touch on the screen but it doesn't . Why ?
Hah — that was really hard to spot.
Your last test in the
if
statement checks for:Should be
Thank you very much ! It work now .
Good evening !
I did a test with class :
Menu :
Button :
Main :
This code must draw a rectangle , but it doesn't . An idea ?
Possibly because you draw the background after the other stuff so it goes on top. Try swapping those statements and see if that helps.