Id reccomend making a button class and a menu class. Create all the buttons you need inside your menu class, then call your menu class through the draw function. Dave’s code is a good starting point, but could be simplified with a menu class. Here’s a sliding button class that i made
Button = class()
function Button:init( vertical, symbol, radius, trigger, defaulty, defaultx, action)
self.vertical = vertical
self.symbol = symbol
self.size = radius
self.trigger = trigger
self.setpos = vec2 (defaultx, defaulty)
self.position = vec2 (defaultx, defaulty)
self.action = action
self.color = color(0, 67, 255, 255)
self.moved = false
end
function Button:draw()
self.touchpos = vec2 (CurrentTouch.x, CurrentTouch.y)
self.touchdist = self.touchpos:dist(self.position)
fill(self.color)
ellipse (self.position.x, self.position.y, self.size)
fill(255, 255, 255, 255)
font("GillSans-Light")
fontSize (35)
text (self.symbol, self.position.x, self.position.y)
if self.touch == true then
if self.vertical == true then
if self.touchdist < self.size/1.5 then
if singler == nil then
singler = self.symbol
else
if singler == self.symbol then
if self.trigger > self.setpos.y then
self.position.y =math.max (CurrentTouch.y, self.setpos.y)
self.position.y =math.min(self.trigger, self.position.y)
else
self.position.y =math.min (CurrentTouch.y, self.setpos.y)
self.position.y= math.max (self.trigger, self.position.y)
end
end
end
end
else
if self.touchdist < self.size/1.5 then
if singler == nil then
singler = self.symbol
else
if singler == self.symbol then
if self.trigger > self.setpos.x then
self.position.x =math.max ( CurrentTouch.x, self.setpos.x)
self.position.x = math.min(self.trigger, self.position.x)
else
self.position.x = math.min (CurrentTouch.x, self.setpos.x)
self.position.x = math.max(self.trigger, self.position.x)
end
end
end
end
end
end
if self.touch == false then
if self.position.y == self.trigger then
self.action()
elseif self.position.x == self.trigger then
self.action()
else
self.position = self.setpos
end
end
end
function Button:touched(touch)
if touch.state == BEGAN then
self.touch = true
elseif touch.state == ENDED then
singler = nil
self.touch = false
end
end
In your menu class’ setup, write this (changing to appropriate values)
button_name(true for up and down slider, false for sideways, "symbol for button", button radius, trigger position, starting y position, starting x position, function() action:action() end)