It looks like you're new here. If you want to get involved, click one of these buttons!
--[[ A generic button class This code is MIT licensed, see <a href="http://www.opensource.org/licenses/mit-license.php" rel="nofollow">http://www.opensource.org/licenses/mit-license.php</a> (C) 2010 - 2011 Gideros Mobile ]] Button = gideros.class(Sprite) function Button:init(upState, downState) self.upState = upState self.downState = downState self.focus = false -- set the visual state as "up" self:updateVisualState(false) -- register to all mouse and touch events self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self) self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self) self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self) self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self) self:addEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self) self:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self) self:addEventListener(Event.TOUCHES_CANCEL, self.onTouchesCancel, self) -- enter frame event self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self) end function Button:onMouseDown(event) if self:hitTestPoint(event.x, event.y) then self.focus = true self:updateVisualState(true) self:dispatchEvent(Event.new('clickDown')) event:stopPropagation() end end function Button:onMouseMove(event) if self.focus then if not self:hitTestPoint(event.x, event.y) then self.focus = false self:updateVisualState(false) end event:stopPropagation() else if self:hitTestPoint(event.x, event.y) then self.focus = true self:updateVisualState(true) end end end function Button:onMouseUp(event) if self.focus then self.focus = false self:updateVisualState(false) self:dispatchEvent(Event.new('clickUp')) event:stopPropagation() end end -- if button is on focus, stop propagation of touch events function Button:onTouchesBegin(event) if self.focus then event:stopPropagation() end end -- if button is on focus, stop propagation of touch events function Button:onTouchesMove(event) if self.focus then event:stopPropagation() end end -- if button is on focus, stop propagation of touch events function Button:onTouchesEnd(event) if self.focus then event:stopPropagation() end end -- if touches are cancelled, reset the state of the button function Button:onTouchesCancel(event) if self.focus then self.focus = false; self:updateVisualState(false) event:stopPropagation() end end -- if the button is focused then dispatch the clickHold event function Button:onEnterFrame(event) if self.focus then self:dispatchEvent(Event.new('clickHold')) end end -- if state is true show downState else show upState function Button:updateVisualState(state) if state then if self:contains(self.upState) then self:removeChild(self.upState) end if not self:contains(self.downState) then self:addChild(self.downState) end else if self:contains(self.downState) then self:removeChild(self.downState) end if not self:contains(self.upState) then self:addChild(self.upState) end end end |
Likes: Tom2012
Comments
function Button:onMouseDown(event) with function Button:onTouchesBegin(event) then
function Button:onMouseMove(event) with function Button:onTouchesMove(event) and
function Button:onMouseUp(event) with function Button:onTouchesEnd(event)
Only difference, that inside touch events you should use event.touch.x and event.touch.y instead of event.x and event.y.
As I said, I haven't tried, but It should/might work
the code,
Also you'll love http://www.tntparticlesengine.com/?cat=13
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
I made the optimization you sugested @atilim, thanks.
Website: http://www.castlegateinteractive.com
https://play.google.com/store/apps/developer?id=Castlegate+Interactive
the simulator and on the iPhone, so why should you use "onTouches"?
Regards,
Marc
hope that it's useful
Likes: phongtt
Basically, the idea is to do most of the processing inside touch event handlers after first taking account of the particular touch id we are working with. However, in order that the button still works inside the desktop player I also have mouse handlers that "fake" up a touch event (with id=0) so I can use the same touch code for either case. I hope that makes sense.
The code below is a snippet from my own button class but it should be easily adaptable back to the original Gideros button if required.
best regards,