Touch

I’m pretty new to Codea I have been looking at the wiki and tutorials I have learned some stuff from them. But
I am still confused with how you would make kinda like a button. For example you have a box on the screen and you have to touch it to drag it around unlike most the example or tutorials I want to make it so I have to drag on it for it to move every tutorial I seen you can drag anywhere on the screen and it moves the box? Sorry I can’t get a good example. :frowning:

Hi! Your explanation is a bit confusing (some ‘.’ are missing in your text?). Can you explain clearly what you want to do?

Lets say I make a ellipse I want to make it so if I tap it it prints text something like that.

This does your spec:


--# Button
Button = class()

function Button:init(str,x,y,w,h)
    self.name = str
    self.x = x
    self.y = y
    self.w = w
    self.h = h
end

function Button:draw()
    ellipse(self.x,self.y,self.w,self.h)
end

function Button:touched(touch)
    local a = (touch.x- self.x)/self.w*2
    local b = (touch.y- self.y)/self.h*2
    if touch.state==BEGAN and (a*a+b*b)<1 then print(self.name) end
end

--# Main
-- test click
function setup()
    but1 = Button("button1",WIDTH/2,HEIGHT/4,300,100)
    but2 = Button("button2",WIDTH/2,HEIGHT/4*3,300,100)
end

function draw()
    background(40, 40, 50)
    strokeWidth(5)
    but1:draw()
    but2:draw()
end

function touched(touch)
    but1:touched(touch)
    but2:touched(touch)
end

That’s just what I needed. Thanks so much your awesome sorry for the bad example.

You are welcome. I just made a small mistake: forgot *2 in the ellipse formula. I’ve correected the post now.