Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Button class is (multitouch) - Gideros Forum

Button class is (multitouch)

olegoleg Member
edited December 2017 in Code snippets
my advanced version:
 --[[
class button v 1.1
Usage:
------------------------------------
	local pause = Button.new(Bitmap.new(Texture.new("img/gui_48.png")))
	stage:addChild(pause)
 
 
	pause:addEventListener("clickDown", 
		function(e)	
		e:stopPropagation()
			knopka:play()
		end)
 
	pause:addEventListener("clickUP", 
		function(e)	
		e:stopPropagation()
			knopka:play()
 
		end)	
 
-------------------------------------
]]
 
Button = gideros.class(Sprite)
 
function Button:init(upState)
	self:addChild(upState)
	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.on=false
end
 
 
 
 function Button:onTouchesBegin(event)
 
	if self:hitTestPoint(event.touch.x, event.touch.y) then
 
		if event.touch.id~=self.focus then 
			self.focus=event.touch.id
 
			if self.on==false then
				self:dispatchEvent(Event.new("clickDown"))
				self.on=true
				self:setColorTransform(2, 2, 2, 1)
			end
			--print("natysnuv"..event.touch.id)
		end
		event:stopPropagation()
	end
 
end
 
 function Button:onTouchesMove(event)
	if self:hitTestPoint(event.touch.x, event.touch.y) then
 
		if self.focus~=event.touch.id then
			self.focus=event.touch.id
			if self.on==false then
				self:dispatchEvent(Event.new("clickDown"))
				self.on=true
				self:setColorTransform(2, 2, 2, 1)
				--print("naviv"..event.touch.id)
			end
		end
	--event:stopPropagation()
	elseif self.focus==event.touch.id then
		--print("vidviv"..event.touch.id)
		self.focus=nil
		if self.on==true then
			self.on=false
			self:setColorTransform(1, 1, 1, 1)
		end
		event:stopPropagation()
	end
 
end
 
 function Button:onTouchesEnd(event)
	if self:hitTestPoint(event.touch.x, event.touch.y) then
 
		self.focus=nil
 
		if self.on==true then
			self:dispatchEvent(Event.new("clickUP"))
			self.on=false
			self:setColorTransform(1, 1, 1, 1)
			--print("vidtysnuv"..event.touch.id)
		end
		event:stopPropagation()
	end
 
end
+1 -1 (+4 / -0 ) Share on Facebook
Sign In or Register to comment.