Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Button class is (multitouch) — Gideros Forum

Button class is (multitouch)

olegoleg Member
edited June 2019 in Code snippets
my advanced version:

outdated version:
 --[[
class button v 1.3 <a href="https://simartinfo.blogspot.com/p/blog-page_23.html" rel="nofollow">https://simartinfo.blogspot.com/p/blog-page_23.html</a>
author (c) <a href="https://www.facebook.com/AndroidMobileGame" 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


latest version here:
https://github.com/razorback456/gideros_tools/blob/master/Button.lua
my games:
https://play.google.com/store/apps/developer?id=razorback456
мій блог по гідерос https://simartinfo.blogspot.com
Слава Україні!
+1 -1 (+8 / -0 )Share on Facebook

Comments

Sign In or Register to comment.