Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Not able to use Button.new - Gideros Forum

Not able to use Button.new

simransimran Member
edited June 2014 in Code snippets
Hello,

I was following Gideros mobile app development bu arturs sosins. The code in it use Button.new(text), I am trying to do exactly similar thing but it gives an error, attempt to index global Button(a nil value)

local startButton = Button.new(startText)
startButton:setPosition(conf.dx - conf.width/3, conf.dy - conf.height/4 )
self:addChild(startButton)
startButton:addEventListener("click", function()
print("Started")
end)

Please help me fix this

Comments

  • yubaroyubaro Member
    edited June 2014
    Hi @simran, try:
                    screenW = application:getContentWidth()
    		screenH = application:getContentHeight()
    		local buttonUp = Bitmap.new(Texture.new("image.png"), true)
    		local buttonDown = Bitmap.new(Texture.new("image.png"), true)
    		buttonUp:setAnchorPoint(0.5, 0.5)	
    		buttonDown:setAnchorPoint(0.5, 0.5)	
    		buttonDown:setAlpha(0.5)
    		local button = Button.new(buttonUp, buttonDown)
    		button:setPosition(screenW * 0.5,screenH * 0.5)
    		self:addChild(button) 
    		button:addEventListener("click", 
    			function()
    				print("Started button")
    			end
    		)
    You must have "button.lua" in your files.
  • simransimran Member
    Thank you for your reply, but I essentially wanted to pass the text and not image. The example is exactly that way in book, I am surprised how could it possibly not work?
  • yubaroyubaro Member
    edited June 2014
    You must have "button.lua" in your files.
    Try:
                   --font = Font.new("font.txt", "font.png", true)
     
                    screenW = application:getContentWidth()
    		screenH = application:getContentHeight()
    		local buttonUp = TextField.new(nil, "TEXT UP", true)  -- nil or font 
    		local buttonDown = TextField.new(nil, "TEXT DOWN", true)
    		--buttonUp:setAnchorPoint(0.5, 0.5)	-- with gideroscodingeasy
    		--buttonDown:setAnchorPoint(0.5, 0.5) -- with gideroscodingeasy	
    		buttonDown:setAlpha(0.5)
    		local button = Button.new(buttonUp, buttonDown)
    		button:setPosition(screenW * 0.5,screenH * 0.5)
    		self:addChild(button) 
    		button:addEventListener("click", 
    			function()
    				print("Started button")
    			end
    		)
  • simransimran Member
    @ar2rsawseen: Yes, I included Button.lua but it necessarily asks for two parameters to be passed to Button.new() but in your book, the example that you have given takes only the text , Would not that work?
  • simransimran Member
    This is the piece of code from book only
    local startText = TextField.new(conf.fontLarge, "Start Game") startText:setTextColor(0xffff00) local startButton = Button.new(startText) startButton:setPosition(conf.width/2, conf.height - 80)
    self:addChild(startButton)
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @simran oh in my book
    there you create your own Button class which accepts only text, yes
  • simransimran Member
    @ar2rswseen: Oh, yes! Silly me . Thanks a ton :) Would like to say it's an awesome book and looking forward to more from you. Thanks :) :D
Sign In or Register to comment.