【问题标题】:How to make buttons stay pressed using corona如何使用电晕使按钮保持按下状态
【发布时间】:2012-12-01 00:54:56
【问题描述】:

我试图让我的按钮在释放后保持“按下”状态。现在我正在为电晕使用改进的按钮模块,我的默认图像是看起来未按下的按钮,而上方的图像被看起来按下的图像替换。

我想要做的是,一旦按下按钮,它就会停留在上方图像上。下面是我的代码是如何为我正在测试的按钮设置的。

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

另外,我有一个函数 (digButtonFunction) 将此按钮的 id 设置为一个变量,用于在用户按下此按钮后按下按钮时运行 if 语句。

【问题讨论】:

    标签: image button replace coronasdk pressed


    【解决方案1】:

    这听起来就像你真正想要的是一个开关。按钮并不是真正从 UI 角度设计的。向下状态只是为了向用户反馈发生了某些操作。

    如果是我,我根本不会使用按钮位,而是使用 display.newImageRect() 加载到图像中并先绘制下态,然后是上态。在每个将隐藏一个或另一个的触摸事件侦听器上构建。我在游戏中为声音开/关按钮执行此操作。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 2018-07-27
      • 2014-09-04
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多