Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Button class - multitouch problem with holding. — Gideros Forum

Button class - multitouch problem with holding.

KarrizKarriz Member
edited August 2013 in General questions
Hi, I'm using a slightly modified button class that allows me to hold a button so that it continuously does an action. This works fine.

The thing is, that I need to be able to hold two buttons down simultaneously, and have both of them firing up their own actions continuously. I did what was suggested there: http://www.giderosmobile.com/forum/discussion/1126/how-to-add-multitouch-support-for-the-button-class (swapped the mouse actions with touch actions)

But, this doesn't work. For example, I'm holding down button A. Then, I also hold down button B. This causes button A action to stop, and button B action only fires up for one frame.


This is the button class I'm currently using:
 
Button = gideros.class(Sprite)
 
function Button:init(upState, downState)
	self.upState = upState
	self.downState = downState
 
	self.focus = false
 
	self:updateVisualState(false)
 
	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)
end
 
function Button:onMouseDown(event)
	if self.focus then
		event:stopPropagation()
	end
end
 
function Button:onMouseMove(event)
if self.focus then
		event:stopPropagation()
	end
end
 
function Button:onMouseUp(event)
	if self.focus then
		event:stopPropagation()
	end
end
 
-- if button is on focus, stop propagation of touch events
function Button:onTouchesBegin(event)
 
 
 
	if self:hitTestPoint(event.touch.x, event.touch.y) then
		self.focus = true
		self:updateVisualState(true)
		self:dispatchEvent(Event.new("down"))
 
		event:stopPropagation()
	end
 
end
 
-- if button is on focus, stop propagation of touch events
function Button:onTouchesMove(event)
 
 
 
 
 
		if self.focus then
		if not self:hitTestPoint(event.touch.x, event.touch.y) then
			self.focus = false;
			self:updateVisualState(false)
			self:dispatchEvent(Event.new("up"))
		end
 
		event:stopPropagation()
	end
 
end
 
-- if button is on focus, stop propagation of touch events
function Button:onTouchesEnd(event)
 
 
 
 
	if self.focus then
		self.focus = false;
		self:updateVisualState(false)
		self:dispatchEvent(Event.new("click"))
		self:dispatchEvent(Event.new("up"))
		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 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
Sign In or Register to comment.