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 January 23 in Code snippets
my advanced version:
 --[[
class button v 1.3 <a href="https://simartinfo.blogspot.com/p/blog-page_23.html" target="_blank" rel="nofollow">https://simartinfo.blogspot.com/p/blog-page_23.html</a>
author (c) <a href="https://www.facebook.com/AndroidMobileGame" target="_blank" rel="nofollow">https://www.facebook.com/AndroidMobileGame</a>
Usage:
------------------------------------
	local btn1= Button.new(Bitmap.new(Texture.new("img/gui_48.png")))
	stage:addChild(btn1)
 
 
	btn1:addEventListener("clickDown", 
		function(e)	
		e:stopPropagation()
                        ---------------------
			print("button Down")
 
                        ---------------------
		end)
 
	btn1:addEventListener("clickUP", 
		function(e)	
		e:stopPropagation()
                        ---------------------
			print("button Up")
 
                        ---------------------
 
		end)	
		btn1:addEventListener("clickMove", 
		function(e)	
   ---------------------
		print(e.x) 
   ---------------------
 
 
		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
		else
 
					local clickMove = Event.new("clickMove")
					clickMove.x = event.touch.x
					clickMove.y = event.touch.y
					self:dispatchEvent(clickMove)
		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 (+7 / -0 ) Share on Facebook

Comments

Sign In or Register to comment.