Widget buttons not working?

Hi,

Can anyone help me with the code below the buttons are showing but the function does not work? If I change the code to default instead of defaultFile the function works but it does not show the button png?


– main.lua


–use the button object from the widget API

local widget = require “widget”

–Hide status bar from the beginning

display.setStatusBar( display.HiddenStatusBar ) 

–Background Image

local screenW, screenH = display.contentWidth, display.contentHeight

local background = display.newImageRect( “images/background.jpg”, screenW, screenH )

background.x = screenW / 2

background.y = screenH / 2

–setup a text object to display the touch outcome

local label = display.newText(“No Button Touch”, 0, 0, nil, 50)

label:setTextColor(255,255,255)

label:translate(100, 100)

–setup the buttons

local leftButton = widget.newButton{

    id = “leftButton”, – id is unique and essential when using event handlers (see addEventListener below)

    defaultFile = “backDefault.png”,

    overFile = “backOver.png”,

    width = 124,

    height = 124,

    --label = “Left Button”

}

local rightButton = widget.newButton{

    id = “rightButton”, – id is again unique

    defaultFile = “forwardDefault.png”,

    overFile = “forwardOver.png”,

    width = 124,

    height = 124,

    --label = “Right Button”

}

local upButton = widget.newButton{

    id = “upButton”, – id is again unique

    defaultFile = “upDefault.png”,

    overFile = “upOver.png”,

    width = 124,

    height = 124,

    --label = “Up Button”

}

leftButton.y = screenH - leftButton.height/2

rightButton.x = 250

rightButton.y = screenH - rightButton.height/2

upButton.x = screenW- upButton.width/2

upButton.y = screenH - upButton.height/2

local function buttonTouch(event)

    --take in the event and get the associated display object

    local myButtons = event.target

    --now find out which button it is

    local nameString = myButtons.id

    

    --use began and ended phases to change text based on touches

    if event.phase == “began” then

        --set the label text to the id

        label.text = nameString

    elseif event.phase == “ended” then

        --back to default

        label.text = “No Button Touch”

    end

return true

end

–add the listener function to the buttons

leftButton:addEventListener(“touch”, buttonTouch)

rightButton:addEventListener(“touch”, buttonTouch)

upButton:addEventListener(“touch”, buttonTouch)

Widget buttons have built in listeners that you add to the options table:

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- --use the button object from the widget API local widget = require "widget" --Hide status bar from the beginning display.setStatusBar( display.HiddenStatusBar ) --Background Image local screenW, screenH = display.contentWidth, display.contentHeight local background = display.newImageRect( "images/background.jpg", screenW, screenH ) background.x = screenW / 2 background.y = screenH / 2 --setup a text object to display the touch outcome local label = display.newText("No Button Touch", 0, 0, nil, 50) label:setTextColor(255,255,255) label:translate(100, 100) local function buttonTouch(event) --take in the event and get the associated display object local myButtons = event.target --now find out which button it is local nameString = myButtons.id --use began and ended phases to change text based on touches if event.phase == "began" then --set the label text to the id label.text = nameString elseif event.phase == "ended" then --back to default label.text = "No Button Touch" end return true end --setup the buttons local leftButton = widget.newButton{ id = "leftButton", -- id is unique and essential when using event handlers (see addEventListener below) defaultFile = "backDefault.png", overFile = "backOver.png", width = 124, height = 124, onEvent = buttonTouch --label = "Left Button" } local rightButton = widget.newButton{ id = "rightButton", -- id is again unique defaultFile = "forwardDefault.png", overFile = "forwardOver.png", width = 124, height = 124, onEvent = buttonTouch --label = "Right Button" } local upButton = widget.newButton{ id = "upButton", -- id is again unique defaultFile = "upDefault.png", overFile = "upOver.png", width = 124, height = 124, onEvent = buttonTouch --label = "Up Button" } leftButton.y = screenH - leftButton.height/2 rightButton.x = 250 rightButton.y = screenH - rightButton.height/2 upButton.x = screenW- upButton.width/2 upButton.y = screenH - upButton.height/2

Also, please be sure to use the code tags to format your code correctly on the forum.  You can find it in the toolbar, it looks like this: <>

Ok will do for the code. Not sure what you mean? Can you give an example?

An example of what?

Sorry not sure what you mean by “Widget buttons have built in listeners that you add to the options table:”

I already included the modified code in my first reply.  See above.

Your an absolute star thank you so much for your help :wink:

Widget buttons have built in listeners that you add to the options table:

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- --use the button object from the widget API local widget = require "widget" --Hide status bar from the beginning display.setStatusBar( display.HiddenStatusBar ) --Background Image local screenW, screenH = display.contentWidth, display.contentHeight local background = display.newImageRect( "images/background.jpg", screenW, screenH ) background.x = screenW / 2 background.y = screenH / 2 --setup a text object to display the touch outcome local label = display.newText("No Button Touch", 0, 0, nil, 50) label:setTextColor(255,255,255) label:translate(100, 100) local function buttonTouch(event) --take in the event and get the associated display object local myButtons = event.target --now find out which button it is local nameString = myButtons.id --use began and ended phases to change text based on touches if event.phase == "began" then --set the label text to the id label.text = nameString elseif event.phase == "ended" then --back to default label.text = "No Button Touch" end return true end --setup the buttons local leftButton = widget.newButton{ id = "leftButton", -- id is unique and essential when using event handlers (see addEventListener below) defaultFile = "backDefault.png", overFile = "backOver.png", width = 124, height = 124, onEvent = buttonTouch --label = "Left Button" } local rightButton = widget.newButton{ id = "rightButton", -- id is again unique defaultFile = "forwardDefault.png", overFile = "forwardOver.png", width = 124, height = 124, onEvent = buttonTouch --label = "Right Button" } local upButton = widget.newButton{ id = "upButton", -- id is again unique defaultFile = "upDefault.png", overFile = "upOver.png", width = 124, height = 124, onEvent = buttonTouch --label = "Up Button" } leftButton.y = screenH - leftButton.height/2 rightButton.x = 250 rightButton.y = screenH - rightButton.height/2 upButton.x = screenW- upButton.width/2 upButton.y = screenH - upButton.height/2

Also, please be sure to use the code tags to format your code correctly on the forum.  You can find it in the toolbar, it looks like this: <>

Ok will do for the code. Not sure what you mean? Can you give an example?

An example of what?

Sorry not sure what you mean by “Widget buttons have built in listeners that you add to the options table:”

I already included the modified code in my first reply.  See above.

Your an absolute star thank you so much for your help :wink: