【问题标题】:Buttons do not work in Lua (Corona)按钮在 Lua (Corona) 中不起作用
【发布时间】:2015-03-20 11:09:34
【问题描述】:

我已经创建了一个游戏,它的游戏部分仍在开发中。但是,该游戏确实可以玩。我使用了一个关于电晕的教程来学习如何制作菜单。菜单显示,音乐播放。但是,给出了这个错误:

Runtime error
addEventListener: listener cannot be nil: nil
stack traceback:
    [C]: in function 'error'
    ?: in function 'getOrCreateTable'
    ?: in function 'addEventListener'
    ?: in function 'addEventListener'
    /Users/jordanmcbride/Desktop/Lua Projects/Tapinator/main.lua:76: in function 'startButtonListeners'
    /Users/jordanmcbride/Desktop/Lua Projects/Tapinator/main.lua:53: in function 'Main'
    /Users/jordanmcbride/Desktop/Lua Projects/Tapinator/main.lua:90: in main chunk

有问题的行是这样的:

function startButtonListeners(action)

   if(action == 'add') then
        playButton:addEventListener('touch', playGame) -- Line 76
        creditsButton:addEventListener('touch', showCredits)
    else
        playButton:removeEventListener('touch', playGame)
        creditsButton:removeEventListener('touch', showCredits)
    end
end

第 53 行:

startButtonListeners('add')

第 90 行:

Main()

这是到目前为止的所有代码(不包括游戏逻辑):

function Main()
    name = display.newImage('title.png', display.contentWidth / 2, 53)
    name:scale( .5, .5 )

    playButton = display.newImage('playButton.png', display.contentWidth / 2, 245)
    playButton:scale( .5, .5 )

    creditsButton = display.newImage('creditsButton.png', display.contentWidth / 2, 305)
    creditsButton:scale( .5, .5 )

    homePage = display.newGroup(name, playButton, creditsButton)

    startButtonListeners('add')
end

local showCredits = {}
function showCredits.touch(e)
    playButton.isVisible = false
creditsButton.isVisible = false
creditsView = display.newImage('credits.png', 0, display.contentHeight)

lastY = name.y
transition.to(name, {time = 300, y = display.contentHeight * 0.5 - title.height - 25})
transition.to(creditsView, {time = 300, y = display.contentHeight * 0.5 + creditsView.height, onComplete = function() creditsView:addEventListener('tap', hideCredits) end})
end

local hideCredits = {}
function hideCredits.touch(e)
    transition.to(creditsView, {time = 300, y = display.contentHeight, onComplete = function() creditsButton.isVisible = true playButton.isVisible = true creditsView:removeEventListener('tap', hideCredits) display.remove(creditsView) creditsView = nil end})
    transition.to(name, {time = 300, y = lastY});
end

function startButtonListeners(action)

    if(action == 'add') then
        playButton:addEventListener('touch', playGame)
        creditsButton:addEventListener('touch', showCredits)
    else
        playButton:removeEventListener('touch', playGame)
        creditsButton:removeEventListener('touch', showCredits)
    end
end

【问题讨论】:

  • playGame函数没有定义,所以playGame变量是nil。为函数添加定义。
  • 感谢@johlo 初学者似乎犯了错误......

标签: android lua coronasdk


【解决方案1】:

正如 johlo 所说,在 startButtonListener 函数期间添加侦听器时未定义 playGame 函数。

此外,如果您尝试制作菜单或 UI,请尝试使用 widgets,例如 newButton 小部件。它使 UI 编码变得更加容易,并且您可以轻松添加多种效果,例如在按下按钮时更改按钮图像。

【讨论】:

  • 老师不希望我们使用任何外部的东西......我们应该“全部手工”完成
  • 好吧,我不相信小部件是外部的,它们是 Corona 提供的功能。不管老师说什么,都可以尝试一下,如果您尝试一下,您将更快地学会编码。不要被老师要你做什么所限制。 :)
  • Touché,我会调查的。您对学习 LUA/Corona 的网站有什么建议吗?因为我没有发现任何有用的东西......如果没有,感谢所有帮助!
  • 当我学习 lua/corona 时,Corona Forum 非常有帮助。他们的documentation 也提供了很多帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-22
  • 1970-01-01
  • 2014-01-31
  • 1970-01-01
  • 2018-06-08
  • 2018-04-10
相关资源
最近更新 更多