I was playing around with my button class and project, and decided to move the function of a touch of the button from the draw function into the touched(touch) function, and it stopped working. It will still work in the draw function, but I was just wondering why not the touched function.
--# Main
-- Button
-- Main
rectMode(RADIUS)
spriteMode(RADIUS)
function setup()
--aloader = SpritelyLoader()
--[[
createbutton = loadstring( readProjectData("Button"))
button = createbutton()
]]
button = Images:getButton()
buttonX = Button("X",WIDTH - 35,HEIGHT - 15,35,15,25,255,0,0,button,close)
buttonKeyboardOpen = Button("Keyboard",WIDTH - 50,HEIGHT - 75,50,15,15,90,175,250,button,showKeyboard)
buttonAlert = Button("Alert",WIDTH - 50,HEIGHT - 125,50,15,15,90,175,250,button,alert,"You pressed the alert button. Alert! Alert! Alert!")
allButton = AllButton({buttonX,buttonKeyboardOpen,buttonAlert})
parameter.watch("buttonAlert.pressed")
parameter.watch("buttonAlert.released")
end
function touched(touch)
end
function draw()
background(127, 127, 127, 255)
-- Button
buttonX:draw()
buttonKeyboardOpen:draw()
buttonAlert:draw()
fill(255, 255, 255, 255)
rect(WIDTH/2,HEIGHT/2,100,100)
pushStyle()
fill(255, 0, 0, 255)
ellipse(CurrentTouch.x,CurrentTouch.y,10)
popStyle()
end
--# Button
-- Button
Button = class()
function Button:init(name,x,y,width,height,fontsize,colourR,colourG,colourB,buttonImage,buttonFunction,buttonFunction01)
self.name = name
self.x = x
self.y = y
self.width = width
self.height = height
self.fontsize = fontsize
self.colourR = colourR
self.colourG = colourG
self.colourB = colourB
self.buttonImage = buttonImage
self.buttonFunction = buttonFunction
self.buttonFunction01 = buttonFunction01
self.buttonFunction02 = buttonFunction02
self.buttonFunction03 = buttonFunction03
self.buttonFunction04 = buttonFunction04
self.pressed = "No"
self.released = "No"
end
function Button:touched(touch)
--if CurrentTouch.state == ENDED
--then self.pressed = "No"
--end
if pressedField(self.x,self.y,self.width,self.height) == true then
self.pressed = "Yes"
end
if releasedField(self.x,self.y,self.width,self.height) == true and self.pressed == "Yes" then
self.released = "Yes"
self.pressed = "No"
else self.released = "No"
self.pressed = "No"
end
if self.released == "Yes"
then if self.buttonFunction01 == nil
then self.buttonFunction()
else
if self.buttonFunction04 ~= nil
then self.buttonFunction(self.buttonFunction01,self.buttonFunction02,self.buttonFunction03,self.buttonFunction04)
end
if self.buttonFunction03 ~= nil
then self.buttonFunction(self.buttonFunction01,self.buttonFunction02,self.buttonFunction03)
end
if self.buttonFunction02 ~= nil
then self.buttonFunction(self.buttonFunction01,self.buttonFunction02)
end
if self.buttonFunction01 ~= nil
then self.buttonFunction(self.buttonFunction01)
end
end
end
end
function Button:draw()
pushStyle()
tint(self.colourR,self.colourG,self.colourB,225)
sprite(self.buttonImage,self.x,self.y,self.width,self.height)
noTint()
fill(255, 255, 255, 255)
fontSize(self.fontsize)
text(self.name,self.x,self.y)
popStyle()
end
--# ButtonAll
-- ControllerAll
-- Forwards each touch event to all the controllers in the table
-- passed to the constructor
AllButton = class(Button)
function AllButton:init(button)
self.button = button
end
function AllButton:touched(t)
for _, c in pairs(self.button) do
c:touched(t)
end
end
function AllButton:draw()
for _, c in pairs(self.button) do
c:draw()
end
end
--# Functions
-- Functions
function pressedField(x,y,width,height)
if CurrentTouch.x <= x + width and CurrentTouch.x >= x - width and CurrentTouch.y <= y + height and CurrentTouch.y >= y - height and CurrentTouch.state == BEGAN
then return true
else return false end
end
function releasedField(x,y,width,height)
if CurrentTouch.x <= x + width and CurrentTouch.x >= x - width and CurrentTouch.y <= y + height and CurrentTouch.y >= y - height and CurrentTouch.state == ENDED
then return true
else return false end
end
There is an image tab for the local image of the button, but unless you want to load this code up, it has no use.
Looking at the code under the Button class, there is a touched(touch) function containing some code. This code used to be at the end of the draw function for the button, where it works. It does not work while in the touched function. Should it be changed to fit in the touched function, or should it stay in the draw function?