Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Checkbox and table — Gideros Forum

Checkbox and table

loves_oiloves_oi Member
edited September 2012 in General questions
I want to build a table which holds : id , name , age .
And checkboxes should be exist in each line. For example , i will select two lines and then clicked submit button. Then it will print these two lines. But how can i do it? How can i do checkboxes ? Is there any example?
Moreover, in order to make a good-looking table , do i have to do it with puting symetric lines manually? Or is there any easy way ?
Thanks in advance...

Comments

  • talistalis Guru
    edited September 2012
    So easy way is to do with a button class. Just when you press it your image should be persistent not like a normal button. Here is an implementation of persistent image with a normal button class.
    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
     
    local checkboxon = Button.new(Bitmap.new(Texture.new("checkbox_checked_image")), Bitmap.new(Texture.new("checkbox_not_checked_image")))
    local checkboxoff= Button.new(Bitmap.new(Texture.new("checkbox_not_checked_image")), Bitmap.new(Texture.new("checkbox_checked_image")))
     
    switch = Switch.new(checkboxon , checkboxoff)
     
    switch:addEventListener("onChange", function(e)
    	print(e.state)
     
        if e.state then
           --not checked state do your job inside here
     
        else
          --checked state, do your job inside here
        end
    end)
  • actually i think this code i posted from one of your examples @ar2rsawseen :)
  • loves_oiloves_oi Member
    edited September 2012
    @talis
    i tried to compile the code in order to see what it looks like.
    But i'm taking error:
    switch.lua:6: bad argument #1 to 'addChild' (Sprite expected, got nil)
    stack traceback:

    What should i do?
    switch.lua:
    Switch = Core.class(Sprite)
     
    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
     
    local checkboxon = Button.new(Bitmap.new(Texture.new("check_tick.png")), Bitmap.new(Texture.new("check_box.png")))
    local checkboxoff= Button.new(Bitmap.new(Texture.new("check_box.png")), Bitmap.new(Texture.new("check_tick.png")))
     
    switch = Switch.new(checkboxon , checkboxoff)
     
    switch:addEventListener("onChange", function(e)
    	print(e.state)
     
        if e.state then
           --not checked state do your job inside here
     
        else
          --checked state, do your job inside here
        end
    end)
    main.lua
     
     
    -- create the up and down sprites for the button
    local upp = Bitmap.new(Texture.new("button_up.png"))
    local down = Bitmap.new(Texture.new("button_down.png"))
     
    -- create the button
    local button1 = Button.new(upp, down)
     
    -- register to "click" event
    local click = 0
    button1:addEventListener("click", 
    	function() 
    		print "Hello World"
    	end)
     
    button1:setPosition(40, 100)
    --stage:addChild(button1)
    -----------------------------------------------------------------------------
    -- create the button
    local button2 = Button.new(upp, down)
     
    -- register to "click" event
    local click = 0
    button2:addEventListener("click", 
    	function() 
    		print "Hello World2"
    	end)
     
    button2:setPosition(40, 100)
    --stage:addChild(button2)
     
    local switch = Switch.new()
    switch:changeState(button1, button2) 
    stage:addChild(switch)
  • HA - So it's not just me who has need of the excessive face palm.

    Even monkeys fall out of trees!
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • ScouserScouser Guru
    Accepted Answer
    @loves_oi: The problem is in your main.lua. on the line
    local switch = Switch.new()
    switch:changeState(button1, button2)
    should be
    local switch = Switch.new(button1, button2)
    switch:changeState()
    maybe??

    Likes: loves_oi

    +1 -1 (+1 / -0 )Share on Facebook
  • @loves_oi, actually the error is exactly as what @scouser has spotted.

    The function init takes two parameters that are set where as the changeState function takes no parameters, but you are passing no parameters to init and passing the params to changeState.

    The error is on line #6, which is
    self:addChild(button1)
    since you have passed nothing, button1 is nil and hence the error. You could modify your functions to check that if New is called without any parameters, you could avoid setting the params and adding the child to the stage and modify the changeState to accept two parameters that can set the button1 and button2 as required.

    Likes: loves_oi

    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
    +1 -1 (+1 / -0 )Share on Facebook
  • loves_oiloves_oi Member
    edited September 2012
    After running , first screen is coming .After clicking ,second (blank) screen is coming. What is wrong with this code ? I attached the zip file of the source code example.zip.what should happen when it works correctly?
    Thanks in advance.
    firstScreen.jpg
    371 x 687 - 23K
    secondScreen.jpg
    371 x 677 - 16K
    zip
    zip
    example.zip
    12K
  • @talis , what should happen when it works correctly? How can i do it?
Sign In or Register to comment.