I can’t seem to catch the ENDED event not matter what I have tried, I even tried an else after getting BEGAN. Here is the current form.
Here is my touch event:
-- touch events function touched(touch) if rightBtn:touched(touch) then if touch.state == BEGAN then print("right") Global.direction = Global.MOVERIGHT end if touch.state == ENDED then print("stop") if Global.direction == Global.MOVERIGHT then Global.direction = Global.NOMOVE end end elseif leftBtn:touched(touch) then if touch.state == BEGAN then print("left") Global.direction = Global.MOVELEFT end if touch.state == ENDED then print("stop") if Global.direction == Global.MOVELEFT then Global.direction = Global.NOMOVE end end elseif fireBtn:touched(touch) then if touch.state == BEGAN then Global.fire = true if gameState == Global.GSTATE_PAUSED then gameState = Global.GSTATE_PLAYING end elseif touch.state == ENDED then print("fire false") Global.fire = false end else if gameState ~= Global.GSTATE_PLAYING then initNewGame() end end end
My draw():
` function draw()
-- background color
background(0, 0, 0)
-- buttons
rightBtn:draw()
leftBtn:draw()
fireBtn:draw()
end`
And finally my Button class:
`Button = class()
function Button:init(x, y, image,...)
-- you can accept and set parameters here
self.x = x
self.y = y
self.w = 32
self.h = 32
self.image = image
self.imageWidth = nil
self.imageHeight = nil
local arg = {...}
self.txt = arg[1];
self.fnt = arg[2];
self.fntSize = arg[3];
end
function Button:draw()
if self.imageWidth then
sprite(self.image, self.x, self.y, self.imageWidth, self.imageHeight)
else
sprite(self.image, self.x, self.y)
end
if self.txt ~= nil then
pushStyle()
fill(90, 71, 71, 255)
font(self.fnt)
fontSize(self.fntSize)
text(self.txt, self.x, self.y)
popStyle()
end
end
function Button:touched(touch)
if touch.state == BEGAN then
if touch.x > self.x - self.w and touch.x < self.x + self.w and
touch.y > self.y - self.h and touch.y < self.y + self.h then
return true
end
end
return false
end`