Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How can I disable a button? — Gideros Forum

How can I disable a button?

AxlFlameAxlFlame Member
edited June 2013 in Game & application design
Hey guys!

Here I was trying to finish my project (the one from where all my previous doubts originated) and I stumbled upon another one...

Here's my idea: I have a minigame where in certain times buttons can be pressed to help advance through the game. The point is, I don't want the buttons to be clickable all the time. How can I enable and disable the click events on my buttons?

So far I have this: The minigame starts with none of the buttons having the "click" event. When an certain timer completes it enables my function "HabilitaBotoes" and that function applies the "click" event listener to each of my buttons. But, when all the buttons have been clicked, I want to lock them again, until the next timer finishes. For that, I created my "DesabilitaBotoes" function. But, for a reason completely unknown for me, my "DesabilitaBotoes" function happens, but the events aren't removed. Why is this happening?

Here is the code for both of the said functions:
function ArmasMontagem:HabilitaBotoes()
	--Listener do botão 1 (Spout)
	wpPiece1:addEventListener("click", 
		function()
			numBox = 1
			self.onClickButton()
		end
	)
	--Listener do botão 2 (HoseAndButton)
	wpPiece2:addEventListener("click", 
		function()
			numBox = 2
			self.onClickButton()
		end
	)
	--Listener do botão 5 (Globe)
	wpPiece5:addEventListener("click", 
		function()
			numBox = 5
			self.onClickButton()
		end
	)
	--Listener do botão 4 (WaterPump)
	wpPiece4:addEventListener("click", 
		function()
			numBox = 4
			self.onClickButton()
		end
	)
	--Listener do botão 3 (Trigger)
	wpPiece3:addEventListener("click", 
		function()
			numBox = 3
			self.onClickButton()
		end
	)
end
 
function ArmasMontagem:DesabilitaBotoes()
	wpPiece1:removeEventListener("click", 
		function()
			numBox = 1
			self.onClickButton()
		end
	)
 
	wpPiece2:removeEventListener("click", 
		function()
			numBox = 2
			self.onClickButton()
		end
	)
 
	wpPiece5:removeEventListener("click", 
		function()
			numBox = 5
			self.onClickButton()
		end
	)
 
	wpPiece4:removeEventListener("click", 
		function()
			numBox = 4
			self.onClickButton()
		end
	)
 
	wpPiece3:removeEventListener("click", 
		function()
			numBox = 3
			self.onClickButton()
		end
	)
end

Comments

  • Hi @AxlFlame,
    You should use something likes Game State to control Game Loop.
    In this case, we only addEventListener and no need to removeEventListener. Logic of clickable or not clickable can be check on onClickButton base on Game State and numBox (click count)
    Coming soon
  • OZAppsOZApps Guru
    Accepted Answer
    try,
    wpPiece3:addEventListener("click",
        function()
            numBox =3
            if self.enabled then
                self.onClickButon()
            end
        end
    )
    and you can enable or disable the clicking by simply this line of code in your app
    wpPiece3.enabled = true -- to make it click
     
    -- OR --
     
    wpPiece3.enabled = false -- to make it not click
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    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
Sign In or Register to comment.