Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
on/off button — Gideros Forum

on/off button

loves_oiloves_oi Member
edited September 2012 in General questions
I need an on/off button. After clicking it , i want to activate/deactivate some property (and if posible change the image). Is there an example which does this job?
Thanks in advance.

Comments

  • loves_oiloves_oi Member
    edited September 2012
    Thanks @ar2rsawseen .I want to use it as a page .I will call it with scenemanager.
    In other page , i call changeScene in this line:
    sceneManager:changeScene("switch", 1, SceneManager.flipWithFade, easing.outBack)
    In switch.lua,i copy pasted your code .It works alone. This is it:
    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")
    	--stage:addChild(switch)
    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
     
     local button1 = Button.new(Bitmap.new(Texture.new("resources/images/button1_up.jpg")), Bitmap.new(Texture.new("resources/images/button1_down.png")))
    local button2 = Button.new(Bitmap.new(Texture.new("resources/images/button2_up.png")), Bitmap.new(Texture.new("resources/images/button2_down.png")))
    switch = Switch.new(button1, button2)
     
    function Switch:getState()
          return self.state
    end
     
    switch:addEventListener("onChange", function(e)
    	print(e.state)
    	--stage:addChild(switch)
        if e.state then
            --show popup
        else
            --hide popuo
        end
    	--stage:addChild(switch)
    end)
     
    stage:addChild(switch)    ---------I think we should put this line to another body
    But when i attempt to integrate change:scene such as the code above , a coincidence appears . I encountered the similar problems before. I added a sample photo.Checkbox page and on/off state page coincide(However , i get no error).To solve this problem i changed the location of the line
    stage:addChild(switch)
    I put this line inside functions to call it when the result page works.But it didn't work.
    How can i solve it ?
    coincidence.png
    335 x 500 - 140K
  • hello
    firstly object switch was not meant to be a scene. So you can't use it in scene manager as scene.
    You should create separate scene, and then add switch to that scene.

    And if you are using scene manager, you don't need to add anything to stage. Only scene manager should be added to stage.
    Everything else should be added to the proper scene instance, where you want it to appear.
  • loves_oiloves_oi Member
    edited September 2012
    OK @ar2rsawseen .I did it .This is a part of main.lua :
     
    sceneManager = SceneManager.new({
    	["optionScreen"] = optionScreen,
    	["results"] = results,
    	["switch"] = switch,
    })
     
    local scenes = {"optionScreen" , "results" , "switch" }
     
    stage:addChild(sceneManager)
    and in results.lua
     
    if (tit == xx)
    then
    sceneManager:changeScene("switch", 1, SceneManager.flipWithFade, easing.outBack)
    print "------We are inside----"
    end
    It prints ------We are inside---- namely, it can enter if block. I think , i should do something another inside switch.lua but don't know what i should do. 
     
    I changed the switch.lua as this:
     
    Switch = Core.class(Sprite)
     
    button1 = Button.new(Bitmap.new(Texture.new("resources/images/button1_up.jpg")), Bitmap.new(Texture.new("resources/images/button1_down.png")))   --line changed 
    button2 = Button.new(Bitmap.new(Texture.new("resources/images/button2_up.png")), Bitmap.new(Texture.new("resources/images/button2_down.png")))   --line changed 
    switch = Switch.new(button1, button2)       --line changed 
     
    --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")
    	self:addChild(switch)          --line changed 
    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
     
     
    switch:addEventListener("onChange", function(e)
    	print(e.state)
     
        if e.state then
            --show popup
        else
            --hide popuo
        end
    end)
    Now , I don't get any error but on/off button appears noWhere (neither in start page nor in next page)
    What should i do ?

  • loves_oiloves_oi Member
    edited September 2012
    Still , i can't find any solution to this problem :S Any suggestion or any idea about this problem or the possible solution??
  • button1 = Button.new(Bitmap.new(Texture.new("resources/images/button1_up.jpg")), Bitmap.new(Texture.new("resources/images/button1_down.png")))   --line changed 
    button2 = Button.new(Bitmap.new(Texture.new("resources/images/button2_up.png")), Bitmap.new(Texture.new("resources/images/button2_down.png")))   --line changed 
    switch = Switch.new(button1, button2)       --line changed
    These lines you're declaring globals and the switch line may actually interfere with your switch scene. At very least, I would not name a global variable the same as your class.
  • petecpetec Member
    edited September 2012
    @ar2rsawseen said 'firstly object switch was not meant to be a scene. So you can't use it in scene manager as scene.' but you still have it as a scene:
    sceneManager = SceneManager.new({
    	["optionScreen"] = optionScreen,
    	["results"] = results,
    	["switch"] = switch,
    })
     
    local scenes = {"optionScreen" , "results" , "switch" }
    When I started with Gideros I had terrible trouble with classes and could hardly understand any of the example code - I certainly couldn't follow the switch code above. So I set to with simpler examples. For example, when I was trying to get to grips with scenemanager, I put simple objects in different scenes (a single sprite with a bitmap, different on each scene so I could tell which scene I was in) and added mouse down eventlisteners to the sprites to change from scene to scene until I got the hang of it.

    I did a similar thing to get the hang of using classes. I made the simplest of classes to just add a sprite with a graphic to the scene and played around to find out how to do use the class to add the sprite to any scene I wanted.

    I think using someone else's code as you have makes learning very difficult. When things don't work, it's hard to know what to do as it's hard to make sense of the code. I would really recommend that you try making the most simple examples of each process you are trying to get working and then, when you've got those under control, start upping the complication. There's nothing like a good play to help clarify things!
  • Thanks friends ,
    i solved the problem after writing a function which contains the below code
    button1 = Button.new(Bitmap.new(Texture.new("resources/images/button1_up.jpg")), Bitmap.new(Texture.new("resources/images/button1_down.png")))   --line changed 
    button2 = Button.new(Bitmap.new(Texture.new("resources/images/button2_up.png")), Bitmap.new(Texture.new("resources/images/button2_down.png")))   --line changed 
    switch = Switch.new(button1, button2)       --line changed
    stage:addChild(switch)
    and the vital point is that : i call this function in results.lua:
    if (tit == xx)
    then
    sceneManager:changeScene("switch", 1, SceneManager.flipWithFade, easing.outBack)
    print "------We are inside----"
    --i called the function which i defined in switch.lua-
    end
Sign In or Register to comment.