Buttons:can't get the label to change...

I’ve been working on a function all day, but for some reason, after I create a button widget, I cannot later call it to change its label (or do anything else to it for that matter, like remove it).

To simplify things, I’m not using my original file, but some code I typed up for testing the problem, which remains even in this simplified example:

local widget = require "widget" local function tappa(event) button:setLabel( "New Button" ) end local button = widget.newButton{label = "The Button", onRelease=tappa} button.x = display.contentCenterX button.y = display.contentCenterY

Now, what I’m expecting is that when the button is pressed, it changes the label on that button to “New Button”. What I get is a button, that when pressed, give me an “attempt to index global ‘button’ (a nil value)” error.

And yes, I know I can use event.target to change it, but the problem in my other program is that I want to change a label on Button A when Button B is pressed or remove it altogether when event C happens, so the example above is more relevant.

I’m using Notepad ++ to edit…

This is good, since you have a small sample that demonstrates the problem, can you click on the “Report a bug” link at the top of the page, tell us what the problem is and include this code.  This way the engineers can track this bug and get it into their work load.

Thanks

Rob

you need to declare

local button above your event function

and remove local from button = widget.newButton

like this

local widget = require "widget"   local button   local function tappa(event)     button:setLabel( "New Button" ) end   button = widget.newButton{label = "The Button", onRelease=tappa} button.x = display.contentCenterX button.y = display.contentCenterY

Yep, that fixed it. Guess I’ve still not got the hang of Local vs. Global.

Mind you, the example I pulled from (http://www.coronalabs.com/blog/2012/05/22/adding-buttons-to-your-games-and-apps/) was none too clear on the matter, which I’m finding is the case a lot, at least for non-programmers trying to learn the language. Thanks, jstrahan.

Here’s another question: how do I change an individual part of the label, like font or fontSize?

Not at home so I can’t say exactly are you talking about after the button is created or when it’s created
If when there’s font = and fontSize=
If after it may be possible or my not it’s been a bit since I used widget button so I’d have to look to be sure

After the button is created. Basically calling the object and saying: ‘Hey, change the label fontSize from the original to X and switch out the background image while you’re at it.’

Hi @jabberwock3,

It sounds like you basically want to create an entirely new button, especially in how you desire to change the background image entirely, along with its font. This kind of low-level “re-creation” isn’t supported at this time, and it probably will not be a priority for the engineers to do so. I suggest that you re-create the button or, if you’re motivated, tinker with the Widget 2.0 open source code and try to accomplish what you need.

Best regards,

Brent

can’t change font or size but you can add xScale and yScale to change size

  local function tappa(event)     button:setLabel( "New Button" )    button.\_label.xScale = 0.5    button.\_label.yScale = 0.5 end  

onsidering my current level level of programming skill, I think I’ll just have to settle with removing and recreating, thanks :slight_smile:

That looks like a good workaround, thanks!

This is good, since you have a small sample that demonstrates the problem, can you click on the “Report a bug” link at the top of the page, tell us what the problem is and include this code.  This way the engineers can track this bug and get it into their work load.

Thanks

Rob

you need to declare

local button above your event function

and remove local from button = widget.newButton

like this

local widget = require "widget"   local button   local function tappa(event)     button:setLabel( "New Button" ) end   button = widget.newButton{label = "The Button", onRelease=tappa} button.x = display.contentCenterX button.y = display.contentCenterY

Yep, that fixed it. Guess I’ve still not got the hang of Local vs. Global.

Mind you, the example I pulled from (http://www.coronalabs.com/blog/2012/05/22/adding-buttons-to-your-games-and-apps/) was none too clear on the matter, which I’m finding is the case a lot, at least for non-programmers trying to learn the language. Thanks, jstrahan.

Here’s another question: how do I change an individual part of the label, like font or fontSize?

Not at home so I can’t say exactly are you talking about after the button is created or when it’s created
If when there’s font = and fontSize=
If after it may be possible or my not it’s been a bit since I used widget button so I’d have to look to be sure

After the button is created. Basically calling the object and saying: ‘Hey, change the label fontSize from the original to X and switch out the background image while you’re at it.’

Hi @jabberwock3,

It sounds like you basically want to create an entirely new button, especially in how you desire to change the background image entirely, along with its font. This kind of low-level “re-creation” isn’t supported at this time, and it probably will not be a priority for the engineers to do so. I suggest that you re-create the button or, if you’re motivated, tinker with the Widget 2.0 open source code and try to accomplish what you need.

Best regards,

Brent

can’t change font or size but you can add xScale and yScale to change size

  local function tappa(event)     button:setLabel( "New Button" )    button.\_label.xScale = 0.5    button.\_label.yScale = 0.5 end  

onsidering my current level level of programming skill, I think I’ll just have to settle with removing and recreating, thanks :slight_smile:

That looks like a good workaround, thanks!