尝试索引GlobalCredits'事件'(零值)

时间:2015-01-21 12:41:27

标签: lua corona

我刚开始在学校学习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]

我看错了,我似乎无法理解如何解决它。其他问题已经说过返回函数返回一个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 )

1 个答案:

答案 0 :(得分:1)

大狼的回答会起作用。但这只是我的经验提示。我喜欢创建函数的一种方法是尝试首先在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函数,因为在定义hideCredits函数时尚未定义showCredit变量。虽然这可能不会影响您当前的游戏,但在将来的应用程序或游戏中,您可能需要调用其他功能内的功能。为了使其正常工作,首先预定义所有函数变量,然后定义所有函数。希望这会有所帮助。