Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
gideros button example problem — Gideros Forum

gideros button example problem

homelesshomeless Member
edited November 2012 in Game & application design
I cant add multiple button instantiations. It shows only the last one. There is supposed to be 4 buttons, but there is only one. This code works with textures but not with Button. Am I missing something?

local label = TextField.new(nil, "Clicked 0 time(s)")
label:setPosition(120, 480)
stage:addChild(label)

-- create the up and down sprites for the button
texUp = Texture.new("button_up_small.png")
up = Bitmap.new(texUp)
texDown = Texture.new("button_down_small.png")
down = Bitmap.new(texDown)

-- create the buttons
posx = 5
posy = 5
cellHeight = texUp:getHeight()
cellWidth = texUp:getWidth()
for i = 1, 2 do
for j = 1, 2 do
button = Button.new(up, down)
button:setPosition(posx, posy)
posx = posx + cellWidth + 5
-- register to "click" event
local click = 0
button:addEventListener("click",
function()
click = click + 1
label:setText("Clicked " .. click .. " time(s)")
end)
stage:addChild(button)
end
posy = posy + cellHeight + 5
posx = 5
end

Dislikes: seppsepp

+1 -1 (+0 / -1 )Share on Facebook

Comments

  • -- create the buttons
    posx = 5
    posy = 5
    cellHeight = texUp:getHeight()
    cellWidth = texUp:getWidth()

    button={}

    for i = 1,4 do
    button[i] = Button.new(up, down)
    button[i]:setPosition(posx, posy)
    posx = posx + cellWidth + 5
    -- register to "click" event
    local click = 0
    button[i]:addEventListener("click",
    function()
    click = click + 1
    label:setText("Clicked " .. click .. " time(s)")
    end)
    stage:addChild(button)
    end
    posy = posy + cellHeight + 5
    posx = 5
    end
  • Tha
    -- create the buttons

    button={}

    for i = 1,4 do
    button[i] = Button.new(up, down)
    button[i]:setPosition(posx, posy)
    posx = posx + cellWidth + 5
    stage:addChild(button)
    posy = posy + cellHeight + 5
    posx = 5
    end
    button={} -- I have already tried array, but nothing has changed.
    stage:addChild(button) -- here it should be button[i], i guess

    The code in your reply has compile errors. Thank you though. I guess there is something wrong with the Button object.
  • hgvyas123hgvyas123 Guru
    edited November 2012 Accepted Answer
    try this
     
    --[[ 
     
    This example demonstrates a generic Button class
     
    This code is MIT licensed, see <a href="http://www.opensource.org/licenses/mit-license.php" rel="nofollow">http://www.opensource.org/licenses/mit-license.php</a>
    (C) 2010 - 2011 Gideros Mobile 
     
    ]]
     
    -- create a label to show number of clicks
    local label = TextField.new(nil, "Clicked 0 time(s)")
    label:setPosition(120, 240)
    stage:addChild(label)
     
    local buttons = {}
    for i=1,4 do
    	-- create the up and down sprites for the button
    	local up = Bitmap.new(Texture.new("button_up.png"))
    	local down = Bitmap.new(Texture.new("button_down.png"))
     
    	-- create the button
    	buttons[i] = Button.new(up, down)
     
    	-- register to "click" event
    	local click = 0
    	buttons[i]:addEventListener("click", 
    		function() 
    			click = click + 1
    			label:setText("Clicked " .. click .. " time(s)")
    		end)
     
    	buttons[i]:setPosition(40, 150*i)
    	stage:addChild(buttons[i])
    end

    :)
  • Thank you, your code is working. But I dont understant why it works. Can you explain a little? Why does it make a difference that writing a local texture or not.
    try this
     
    --[[ 
     
    This example demonstrates a generic Button class
     
    This code is MIT licensed, see <a href="http://www.opensource.org/licenses/mit-license.php" rel="nofollow">http://www.opensource.org/licenses/mit-license.php</a>
    (C) 2010 - 2011 Gideros Mobile 
     
    ]]
     
    -- create a label to show number of clicks
    local label = TextField.new(nil, "Clicked 0 time(s)")
    label:setPosition(120, 240)
    stage:addChild(label)
     
    local buttons = {}
    for i=1,4 do
    	-- create the up and down sprites for the button
    	local up = Bitmap.new(Texture.new("button_up.png"))
    	local down = Bitmap.new(Texture.new("button_down.png"))
     
    	-- create the button
    	buttons[i] = Button.new(up, down)
     
    	-- register to "click" event
    	local click = 0
    	buttons[i]:addEventListener("click", 
    		function() 
    			click = click + 1
    			label:setText("Clicked " .. click .. " time(s)")
    		end)
     
    	buttons[i]:setPosition(40, 150*i)
    	stage:addChild(buttons[i])
    end

    :)
  • ok, there is no problem with preloading the textures but preloading the bitmap is the problem see the below code
     
    local label = TextField.new(nil, "Clicked 0 time(s)")
    label:setPosition(120, 480)
    stage:addChild(label)
     
    -- create the up and down sprites for the button
    texUp = Texture.new("button_up.png")
     
    texDown = Texture.new("button_down.png")
     
     
    -- create the buttons
    posx = 5
    posy = 5
    cellHeight = texUp:getHeight()
    cellWidth = texUp:getWidth()
    for i = 1, 2 do
    for j = 1, 2 do
    up = Bitmap.new(texUp)
    down = Bitmap.new(texDown)
    button = Button.new(up, down)
    button:setPosition(posx, posy)
    posx = posx + cellWidth + 5
    -- register to "click" event
    local click = 0
    button:addEventListener("click",
    function()
    click = click + 1
    label:setText("Clicked " .. click .. " time(s)")
    end)
    stage:addChild(button)
    end
    posy = posy + cellHeight + 5
    posx = 5
    end
    it will work fine the problem was you had created

    up = Bitmap.new(texUp)
    down = Bitmap.new(texDown)

    out side of the for loops and then you are adding it to 4 different parents [in our case Button.new()] and because of this all the problems occured as any sprite should have only one parent when you try to add it to different parent it will first removed from the previous parent and then adding in new parent

    i hope it will help

    :)
  • also i recommend to use array and store all four buttons in that array else you will loose the access of first 3 buttons and only fourth button can be accessible due to the all of them have same name or id or whatever

    :)

    Likes: rodri

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.