I am just playing around with the button sample and wonder why it registers BOTH, mouse and touch events to the button. The touch events seem to be useless in this example.
Wouldn't it be enough to use mouse events only to create a button -like this?
Button = Core.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.onTouch, self)
self:addEventListener(Event.MOUSE_MOVE, self.onTouch, self)
self:addEventListener(Event.MOUSE_UP, self.onTouch, self)endfunction Button:onTouch(event)local phase = event.type-- MOUSE DOWNif phase =="mouseDown"thenif self:hitTestPoint(event.x, event.y)then
self.focus =true
self:updateVisualState(true)
event:stopPropagation()endelseif phase =="mouseMove"thenif self.focus thenifnot self:hitTestPoint(event.x, event.y)then
self.focus =false
self:updateVisualState(false)end
event:stopPropagation()endelseif phase =="mouseUp"thenif self.focus then
self.focus =false
self:updateVisualState(false)
self:dispatchEvent(Event.new("click"))-- button is clicked, dispatch "click" event
event:stopPropagation()endendend-- if state is true show downState else show upStatefunction Button:updateVisualState(state)if state thenif self:contains(self.upState)then
self:removeChild(self.upState)endifnot self:contains(self.downState)then
self:addChild(self.downState)endelseif self:contains(self.downState)then
self:removeChild(self.downState)endifnot self:contains(self.upState)then
self:addChild(self.upState)endendend
@MauMau I think that was for historical purpose/reference, when Gideros was first released, mouse events were only dispatched on Desktop, and touch events only dispatched on mobiles (or something like that, I can't even remember now), so in most cases you needed to handle them both
@maumau there is a glaring error in your code. Since using Lua frameworks touch handlers is the most common routines, this stood out immediately.
in the phase == "mouseMove" you are checking for if self.focus and if the touch is outside of the button, you set self.focus to false, but now the user can *NEVER* comback on to the object in that touch lifetime.
so you might have to separate that into when the touch is outside and when the touch is inside but not alter the self.focus and similarly in the mouseUp check again for where the touch was inside or outside.
Comments
http://appcodingeasy.com/Gideros-Mobile/Gideros-mobile-button-class
You need to download library, add it to project and use it as described
Likes: gyrosp
It seems as tutorials and docs for Gideros are spread throughout the whole internet .
http://giderostutorials.com/
Wouldn't it be enough to use mouse events only to create a button -like this?
in the phase == "mouseMove" you are checking for if self.focus and if the touch is outside of the button, you set self.focus to false, but now the user can *NEVER* comback on to the object in that touch lifetime.
so you might have to separate that into when the touch is outside and when the touch is inside but not alter the self.focus and similarly in the mouseUp check again for where the touch was inside or outside.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
http://docs.giderosmobile.com/reference/gideros/Sprite/Event.TOUCHES_CANCEL#Event.TOUCHES_CANCEL