【问题标题】:Corona. I can't access to a local variable from a function电晕。我无法从函数访问局部变量
【发布时间】:2026-01-21 11:00:01
【问题描述】:

SDK 电晕 我想执行 --> movboton:setSequence("apretado" )

知道为什么吗? 谢谢。

botonnormal = graphics.newImageSheet( "buttonstart.png", { width=340, height=338, numFrames=2 } )
botonapretado = graphics.newImageSheet ( "buttonstart.png", { width=340, height=338, numFrames=2 } )


local movboton
local seqBoton = {
    { name="normal", sheet=botonnormal, start=1, count=1, time=1000},
    { name="apretado", sheet=botonapretado, start=2, count=2, time=1100}
}

movboton = display.newSprite(botonnormal, seqBoton)
movboton.x = display.contentWidth / 2
movboton.y = display.contentHeight / 2
movboton.xScale = .5
movboton.yScale = .5

如果我把代码放在这里,它可以工作,但是从我触摸屏时调用的函数来看,它给了我 nil 错误。

movboton:setSequence("pretado")
movboton:play()

local function apretato(event)
    --print("apretado")
    if event.phase == "began" then
        print("start")
        --storyboard.gotoScene("game", "fade", 400)
        movboton:setSequence( "apretado" )
        movboton:play()
    end

end
function scene:enterScene(event)
   btninvisible:addEventListener( "touch", apretato )
end

所有代码如下:

-- requerimientos


local storyboard = require("storyboard")
local scene = storyboard.newScene()

--Background

function scene:createScene(event)
    local screenGroup = self.view  

    local background = display.newImage("start.png")
    screenGroup:insert(background) 


    city2 = display.newImage("city2.png")
    city2:setReferencePoint(display.BottomLeftReferencePoint)
    city2.x = 0
    city2.y = 320
    screenGroup:insert(city2)  




    --PERSONAJE
    --Imagenes en forma de sheet
    botonnormal = graphics.newImageSheet( "buttonstart.png", { width=340, height=338, numFrames=2 } )
    botonapretado = graphics.newImageSheet ( "buttonstart.png", { width=340, height=338, numFrames=2 } )

    --Simulacion de andar del personaje
    local movboton
    local seqBoton = {
        { name="normal", sheet=botonnormal, start=1, count=1, time=1000},
        { name="apretado", sheet=botonapretado, start=2, count=2, time=1100}     --, loopCount =0
    }


    --Iniciamos
    movboton = display.newSprite(botonnormal, seqBoton)
    movboton.x = display.contentWidth / 2
    movboton.y = display.contentHeight / 2
    movboton.xScale = .5
    movboton.yScale = .5


    mybutton = display.newImage("button.png")
    mybutton.x = display.contentWidth /2
    mybutton.y = display.contentHeight -75
    mybutton.xScale = .3
    mybutton.yScale = .3

    btninvisible = display.newImage("botonopacityzero.png")
    btninvisible.x = display.contentWidth /2
    btninvisible.y = display.contentHeight /2
    btninvisible.xScale = .5
    btninvisible.yScale = .5
    btninvisible.alpha = 0.2 --opacidad

end


function start(event)
    if event.phase == "began" then
    --print("start")
    storyboard.gotoScene("game", "fade", 400)
end

end

local function apretato(event)
    --print("apretado")
    if event.phase == "began" then
        print("start")
        --storyboard.gotoScene("game", "fade", 400)
        movboton:setSequence( "apretado" )
        movboton:play()
    end

end

function scene:enterScene(event)

    --background:addEventListener("touch", start)

    mybutton:addEventListener( "touch", start )

    btninvisible:addEventListener( "touch", apretato )

end

function scene:exitScene(event)

    --background:removeEventListener("touch", start)
    mybutton:removeEventListener( "touch", start )
    --mybutton.destroy()

    mybutton:removeSelf()
    mybutton = nil

end

function scene:destroyScene(event)

end

scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)


return scene

【问题讨论】:

    标签: android lua coronasdk


    【解决方案1】:

    据我所知,永远不要将movboton 设置为任何东西,所以当您尝试调用movboton:setSequence( "apretado" ) 时,它自然是nil。无论你把它放在哪里,我都看不出这样的调用会在这段代码中起作用......

    如果您需要更多帮助,请显示更多代码。如果有可行的情况和不可行的情况,则将两者都包括在内,以便我们进行比较。


    以下代码在这两种情况下都存在吗?

    movboton = display.newSprite(botonnormal, seqBoton)
    movboton.x = display.contentWidth / 2
    movboton.y = display.contentHeight / 2
    movboton.xScale = .5
    movboton.yScale = .5
    

    您的原始帖子中没有。如果没有该代码,您将收到错误消息。当您包含上面显示的代码时,它是否仍然失败并出现相同的错误?当您尝试运行它时,您能否完全按照文件中的内容显示整个代码?


    好的,现在很清楚为什么是nil

    movboton 变量在createScene 函数中声明为local

    function scene:createScene(event)
        ...
        local movboton
        ...
    end
    

    local 关键字告诉 Lua 这个变量只在上述函数内部可用。这意味着如果您尝试从该函数外部 的任何地方访问movboton,那么Lua 将找不到它。就 Lua 而言,在 createScene 代码块之外没有名为 movboton 的变量。

    你的movboton:setSequence( "apretado" ) 行在createScene 函数之外,所以,同样,就 Lua 而言,代码中此时没有变量 movboton,所以你取而代之的是nil(没有任何意义)。

    您需要做的是更改您的代码,以便可以从您的apretato 函数内部看到movboton。最简单的方法就是将local movboton 行移动到local scene = storyboard.newScene() 下方:

    local storyboard = require("storyboard")
    local scene = storyboard.newScene()
    local movboton
    

    如果改为在此处声明,则在包含apretato 的范围内声明,这意味着apretato 将能够看到它。

    You can read more about scopes in Lua here. 作用域是一个或多或少在所有编程语言中都非常重要的概念,因此值得花时间尝试了解它们的工作原理。

    祝你好运:)

    【讨论】:

    • 是的,它仍然给我同样的错误,现在你可以看到所有的代码。谢谢@Supr
    • 是的,这是问题所在,感谢您的帮助。谢谢:) @Supr