How do you scale a button widget by its id?

Hi,

I have a simple question: I am using the button widget like so:

local button = widget.newButton({ id = "some-button", width = 60, height = 60, defaultFile = "images/button.png", onEvent = onButtonPressed });

and I want to apply a scale it when it’s pressed. I’m thinking of doing this here:

local function onButtonPressed(event) if (event.phase == "began") then -- Scale button object button:scale(0.8, 0.8) end if (event.phase == "ended") then -- Do something end end

I don’t supposed the button object is what is being passed into the function is it? It’s just triggering an event. Is there a way to scale the button when pressed began using the button id?

Thanks.

You usually can get the object causing the event from event.target, so

[Lua]

event.target:scale( 0.8, 0.8 )

[/Lua]

should do what you want.

This does what I want but I’m not achieving what I ultimately want to do.

What I want to do is scale the button when it’s pressed and then scale it back when the player “unpresses” it or releases it. The problem is if the user slides their finger off the button then released the screen, there is no call to “unpress” the button.

I have a global script

M.buttonPressed = function(event) event.target:scale(0.8, 0.8); end M.buttonUnpressed = function(event) event.target:scale(1.2, 1.2); end

and a button widget in a scene

local global = require("scripts.globals"); local function someButton(event) if (event.phase == "began") then global.buttonPressed(event); end if (event.phase == "ended") then global.buttonUnpressed(event); end end

You can do this, by setting the focus for your button during the “began” phase and clear it on ended (and cancelled).

[lua]

local function someButton(event)

  if (event.phase == “began”) then

    display.getCurrentStage():setFocus( event.target )

    global.buttonPressed(event);

  end

  if (event.phase == “ended” or event.phase == “cancelled” ) then

    display.getCurrentStage():setFocus( nil )

    global.buttonUnpressed(event);

  end

end

[/lua]

There’s some more information here https://docs.coronalabs.com/api/type/StageObject/setFocus.html

You usually can get the object causing the event from event.target, so

[Lua]

event.target:scale( 0.8, 0.8 )

[/Lua]

should do what you want.

This does what I want but I’m not achieving what I ultimately want to do.

What I want to do is scale the button when it’s pressed and then scale it back when the player “unpresses” it or releases it. The problem is if the user slides their finger off the button then released the screen, there is no call to “unpress” the button.

I have a global script

M.buttonPressed = function(event) event.target:scale(0.8, 0.8); end M.buttonUnpressed = function(event) event.target:scale(1.2, 1.2); end

and a button widget in a scene

local global = require("scripts.globals"); local function someButton(event) if (event.phase == "began") then global.buttonPressed(event); end if (event.phase == "ended") then global.buttonUnpressed(event); end end

You can do this, by setting the focus for your button during the “began” phase and clear it on ended (and cancelled).

[lua]

local function someButton(event)

  if (event.phase == “began”) then

    display.getCurrentStage():setFocus( event.target )

    global.buttonPressed(event);

  end

  if (event.phase == “ended” or event.phase == “cancelled” ) then

    display.getCurrentStage():setFocus( nil )

    global.buttonUnpressed(event);

  end

end

[/lua]

There’s some more information here https://docs.coronalabs.com/api/type/StageObject/setFocus.html