How to make buttons stay pressed using corona

喜夏-厌秋 提交于 2019-12-13 07:47:09

问题


I am trying to get my buttons to stay "pressed" once it is released. Right now I am using the improved Buttons Module for corona and I have the default image being the button looking unpressed, and the over image being replaced by an image that looks pressed.

What I am trying to do is once the button is pressed, it stays on the over image. Here is how my code is set up for the button I am testing it on.

local digButton = buttons.newButton{
    default = "digButton.png",
    over = "digButtonPressed.png",
    onEvent = digButtonFunction,
    id = "dig"
    }
    digButton:setReferencePoint(display.CenterReferencePoint)
    digButton.x = display.contentWidth/5
    digButton.y = display.contentHeight/1.9

Also, I have a function (digButtonFunction) that sets the id of this button to a variable to be used to run an if statement for when the user pushes a button following this one.


回答1:


This sounds to me like what you really want is a switch. Buttons are not really designed from a UI perspective to do that. The down-state is there just to give feedback to the user that some action happened.

If it were me, I'd not use the button bit at all, but load in to images using display.newImageRect() and draw the downstate first, then the upstate. Built a touch event listener on each one that will hide one or the other. I do this in my games for my sound on/off buttons.

local soundOn = true 
local soundOnBtn, soundOffBtn

local function soundToggle(event)
    if soundOn then
        soundOn = false
        soundOnBtn.isVisible = false
        soundOffBtn.isVisible = true
    else
        soundOn = true
        soundOnBtn.isVisible = true
        soundOffBtn.isVisible = false
    end
    return true
end
soundOnBtn = display.newImageRect("images/switch_on.png", 46, 36)
soundOnBtn.x = display.contentWidth / 2 + 25
soundOnBtn.y = display.contentHeight / 2 - 15
group:insert(soundOnBtn)
soundOnBtn:addEventListener("tap", soundToggle)

soundOffBtn = display.newImageRect("images/switch_off.png", 46, 36)
soundOffBtn.x = display.contentWidth / 2 + 25
soundOffBtn.y = display.contentHeight / 2 - 15
group:insert(soundOffBtn)
soundOffBtn:addEventListener("tap", soundToggle)


soundOffBtn.isVisible = false


来源:https://stackoverflow.com/questions/13854684/how-to-make-buttons-stay-pressed-using-corona

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!