【问题标题】:attempt to index global 'playButton' (a nil value)尝试索引全局“playButton”(零值)
【发布时间】:2020-03-08 10:46:51
【问题描述】:

几周前我刚刚开始了 LUA。我能够创建一个带有分数的简单角色跳跃,然后使用 Corona Simulator 运行它。但是我想创建一个菜单系统,所以从加载 menu.lua 的 main.lua 开始,它会从这里转到 Start(即开始游戏,即到 game.lua),因此我可以玩游戏。但它没有出现。我已尝试添加 eventListeners,如下所示 o

function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
        -- Code here runs when the scene is still off screen (but is about to come on screen)

    elseif ( phase == "did" ) then
        -- Code here runs when the scene is entirely on screen
        startButton:addEventListener( "tap", gotoGame )
        highScoresButton:addEventListener( "tap", gotoHighScores )

    end
end

除此之外,我不明白为什么菜单系统不会转到 Game.lua,因此我可以玩游戏。另外我不明白为什么加载menu.lua时会出现错误我使用了场景和作曲家管理库,理论上它应该可以工作。

图像文件在一个文件夹中

ma​​in.lua 文件 1

local composer = require( "composer" )

-- Hide status bar
display.setStatusBar( display.HiddenStatusBar )

-- Seed the random number generator
math.randomseed( os.time() )

-- Go to the menu screen
composer.gotoScene("menu")

Menu.lua 文件 2

local composer = require( "composer" )

local scene = composer.newScene()

-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------

local function gotoGame()
    composer.gotoScene( "game" )
end

local function gotoHighScores()
    composer.gotoScene( "highscores" )
end


-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------

-- create()
function scene:create( event )

    local sceneGroup = self.view
    -- Code here runs when the scene is first created but has not yet appeared on screen

    local background = display.newImageRect( sceneGroup, "background.png", 800, 1400 )
    background.x = display.contentCenterX
    background.y = display.contentCenterY

    local title = display.newImageRect( sceneGroup, "title.png", 500, 80 )
    title.x = display.contentCenterX
    title.y = 200

    local startButton = display.newText( sceneGroup, "Start", display.contentCenterX, 700, native.systemFont, 44 )
    startButton:setFillColor( 0.82, 0.86, 1 )

    local highScoresButton = display.newText( sceneGroup, "High Scores", display.contentCenterX, 810, native.systemFont, 44 )
    highScoresButton:setFillColor( 0.75, 0.78, 1 )

    startButton:addEventListener( "tap", gotoGame )
    highScoresButton:addEventListener( "tap", gotoHighScores )
end


-- show()
function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
        -- Code here runs when the scene is still off screen (but is about to come on screen)

    elseif ( phase == "did" ) then
        -- Code here runs when the scene is entirely on screen
        startButton:addEventListener( "tap", gotoGame )
        highScoresButton:addEventListener( "tap", gotoHighScores )

    end
end


-- hide()
function scene:hide( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
        -- Code here runs when the scene is on screen (but is about to go off screen)

    elseif ( phase == "did" ) then
        -- Code here runs immediately after the scene goes entirely off screen

    end
end


-- destroy()
function scene:destroy( event )

    local sceneGroup = self.view
    -- Code here runs prior to the removal of scene's view

end


-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------

return scene

game.lua 文件 3

local composer = require("composer")

local scene = composer.newScene()
display.setStatusBar(display.HiddenStatusBar)
local physics = require "physics"





function updateScore()
    score=score+1
    scoreText.text = "Score: "..score
    scoreText.x, scoreText.y=80,40
end



function onTouch(event)
    if(event.phase=="began") then
        if(event.x<player.x) then
            player:setLinearVelocity(-30,-200)
            updateScore()
        else
            player:setLinearVelocity(30,-200)
            updateScore()
        end
    end
end


-- create()
function scene:create( event )

    local sceneGroup = self.view
    -- Code here runs when the scene is first created but has not yet appeared
    physics.pause()
    score=0
    local scoreText
    local ground = display.newImage("ground.jpg")
    local player = display.newImage("player.png")


end

-- show()
function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
        -- Code here runs when the scene is still off screen (but is about to come on screen)

    elseif ( phase == "did" ) then
        -- Code here runs when the scene is entirely on screen
        physics.start()
        scoreText=display.newText("Score:0",0,0,native.systemFont,40)
        scoreText.x, scoreText.y=80,40

        ground.x=460
        ground.y=1300
        physics.addBody(ground, "static")


        player.x=360
        player.y=120

        physics.addBody(player)
        Runtime:addEventListener("touch", onTouch)
    end
end


-- hide()
function scene:hide( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
        -- Code here runs when the scene is on screen (but is about to go off screen)

    elseif ( phase == "did" ) then
        -- Code here runs immediately after the scene goes entirely off screen

    end
end


-- destroy()
function scene:destroy( event )

    local sceneGroup = self.view
    -- Code here runs prior to the removal of scene's view

end


-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------

return scene

【问题讨论】:

  • 显示 menu.lua 和 game.lua 的所有代码
  • 提供的代码没有索引 playButton。通常错误消息带有文件名和行。
  • 这是我所有的代码
  • playButton 没有在任何地方定义,所以我不知道你是怎么得到这个错误的。

标签: lua coronasdk


【解决方案1】:

当你用

创建场景时
function scene:create( event )

您总是希望将对象(本地)插入到场景组中这样做

sceneGroup:insert( object )
sceneGroup:insert( object2 )

我也怀疑事件监听器是触摸而不是点击(通过查看文档):

startButton:addEventListener( "touch", gotoGame )
highScoresButton:addEventListener( "touch", gotoHighScores )

无论如何,我建议您访问documentation:它涵盖了所有内容,而且非常丰富。

【讨论】:

    猜你喜欢
    • 2013-06-07
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 2017-01-10
    • 2015-02-18
    • 2020-04-27
    相关资源
    最近更新 更多