Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Button on/off state — Gideros Forum

Button on/off state

twisttaptwisttap Member
edited August 2012 in General questions
How can I implement a on/off button with a shortest way ? It will basically will be a switch, when its pressed it will show a popup and when pressed again, it will hide the popup.

Thanks
Tagged:

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited August 2012 Accepted Answer
    Modified example to working two state switch which uses Button class
    Switch = Core.class(Sprite)
     
    --constructor for Button
    function Switch:init(button1, button2)
    	self.button1 = button1
    	self.button2 = button2
    	self:addChild(button1)
        self.state = false
        self.button1:addEventListener("click", self.changeState, self)
        self.button2:addEventListener("click", self.changeState, self)
        self.onChange = Event.new("onChange")
    end
     
    function Switch:changeState()
        self.state = not self.state
    	if self.state then
    		if self:contains(self.button1) then
    			self:removeChild(self.button1)
    		end
    		self:addChild(self.button2)
    	else
    		if self:contains(self.button2) then
    			self:removeChild(self.button2)
    		end
    		self:addChild(self.button1)
    	end
        self.onChange.state = self.state
        self:dispatchEvent(self.onChange)
    end
     
    function Switch:getState()
          return self.state
    end
    Usage:
    local button1 = Button.new(Bitmap.new(Texture.new("button1_up.png")), Bitmap.new(Texture.new("button1_down.png")))
    local button2 = Button.new(Bitmap.new(Texture.new("button2_up.png")), Bitmap.new(Texture.new("button2_down.png")))
     
    local switch = Switch.new(button1, button2)
     
    switch:addEventListener("onChange", function(e)
    	print(e.state)
        if e.state then
            --show popup
        else
            --hide popuo
        end
    end)
     
    stage:addChild(switch)
  • Actually this is not I was asking, I want an on off switch with a single button which is like when I press button1 it will switch to active state and stay there and vice versa ^^
  • This comment probably is for my previous example, right?
  • talistalis Guru
    edited August 2012
    I had something like this in my mind @twisttap , of course @ar2rsawseen 's solution is working and can be more better but just to approach the problem from different point of view. (More structured programming than OOP)

    1-I will add one function to the button class.
     
    function Button:changeState(sprite)
    if self:contains(self.upState) then
    			self:removeChild(self.upState)
    end
    if self:contains(self.downState) then
    			self:removeChild(self.downState)
    end
    self.upState = sprite
    self.downState = sprite
    self:updateVisualState(true)
    end
    2-I will call my function on press function of my button. And with a global bolean variable i will check if it is pressed or not. I f yes i will call it like this.
    myButton:changeState(OffSprite)
    3-If no:
    myButton:changeState(OnSprite)
Sign In or Register to comment.