尝试索引 GlobalCredits 'event'(零值)

Attempt to Index GlobalCredits 'event' (a nil value)

我刚开始在学校学习 LUA,但我在互联网上找不到很多有用的教程来帮助我学习。我制作了一个简单的游戏(我意识到它还不能运行)和一个主菜单。但是,当我尝试启动应用程序时,它给了我这个错误:

/Users/jordanmcbride/Desktop/Lua Projects/Tapinator/main.lua:47: attempt to index global 'showCredits' (a nil value)
stack traceback:
/Users/jordanmcbride/Desktop/Lua Projects/Tapinator/main.lua:47: in main chunk
[Finished in 9.4s]

我查看了错误,但似乎无法理解如何修复它。其他问题说了一些关于 returning 函数 returning 一个 nil 值,我应该在最后添加一个 return 语句,但这也不起作用。

这是第 47 行的代码。

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

这是我的完整代码,以防问题出在其他地方:

display.setStatusBar(display.HiddenStatusBar)

radius = 40

smallTime = 200
bigTime = 800

score = 0
scoreInc = 2000

--HomePage
local name
local playButton
local creditsButton
local homePage

--Credits
local creditsPage


--Sounds
local circleSpawn = audio.loadSound( "circle_spawn.wav" )
local circleTap = audio.loadSound( "circle_tap.wav" )

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

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

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



Main()


printScore = display.newText("Score: " .. tostring(score), display.contentWidth-80, 40, native.systemFontBold, 20)

-- A function that creates random circles
function generateCircle ()

    -- Creates a new circle between 0 (the left most bounds) and the width of the display (being the content width), and also
    -- 0 (the upper most bounds) and the height of the display (being the content height). The radius of the circle is 'radius'
    x = math.random(radius, display.contentWidth-radius)
    y = math.random(80, display.contentHeight)

    score = score + scoreInc

    myCircle = display.newCircle( x, y, radius )
    myCircle:setFillColor( math.random(), math.random(), math.random() )

    delayTime = math.random(smallTime, bigTime)

    score = score + scoreInc

    printScore.text = "Score:"..tostring(scores)

    local spawnChannel = audio.play( circleSpawn )
    timer.performWithDelay( delayTime, generateCircle )

end

generateCircle()

function myCircle:touch( event )

    local tapChannel = audio.play( circleTap )
    myCircle:removeSelf()

end

myCircle:addEventListener( "touch", myCircle )

greatwolf 的回答可以。但只是我的经验提示。我喜欢创建函数的一种方法是尝试首先在 lua 文件的顶部附近定义函数的名称。然后我稍后会在文件中定义函数。像这样:

--function preallocation
local onPlayTap
local onSoundOnTap
local onSoundOffTap
local onCreditsTap
local onHelpTap

---------------------------------------------------------------------------------
-- Custom Function Definitions
---------------------------------------------------------------------------------
--Called when Sound On Button is tapped, turn off sound
onSoundOnTap = function(event)

end

--Called when Sound Off Button is tapped, turn on sound
onSoundOffTap = function(event)

end

--Called when Credits button is tapped, shows credits
onCreditsTap = function(event)

end

--Called when Help button is tapped, shows help
onHelpTap = function(event)

end

--Callback to Play button. Moves scene to Level Picker Scene
onPlayTap = function(event)

end

这样做是允许文件中的任何其他函数调用每个函数。如果你按照你这样做的方式通过在函数之前添加函数名称来做到这一点:

local showCredits = {}
function showCredits.touch(e)

end

local hideCredits = {}
function hideCredits.touch(e)

end

您的 showCredit 函数将无法调用其下方的 hideCredits 函数,因为在定义 showCredit 函数时尚未定义 hideCredits 变量.虽然这可能不会影响您当前的游戏,但在未来的应用或游戏中,您可能需要在其他函数内部调用函数。要使其正常工作,请先预定义所有函数变量,然后再定义所有函数。希望这有帮助。