Lua中的条件分支是';你为什么不工作?

Lua中的条件分支是';你为什么不工作?,lua,sublimetext3,coronasdk,Lua,Sublimetext3,Coronasdk,我加入这个社区是因为它看起来很棒,而且我是一个新的程序员,所以我想尝试按照这样一个好社区的建议快速地提高自己。:) 我有一个关于lua编码的问题——现在,我正在使用Corona SDK和Sublime Text 3编码一个移动启动屏幕和一个标题屏幕。我从titleScreen=false开始,这样我就可以运行启动屏幕的代码,使之成为现实。但是,当我运行完启动屏幕的代码并使titleScreen=true时,titleScreen的条件分支就不起作用了!请帮帮我:(我已经做了大量的测试,但无法找出

我加入这个社区是因为它看起来很棒,而且我是一个新的程序员,所以我想尝试按照这样一个好社区的建议快速地提高自己。:)

我有一个关于lua编码的问题——现在,我正在使用Corona SDK和Sublime Text 3编码一个移动启动屏幕和一个标题屏幕。我从titleScreen=false开始,这样我就可以运行启动屏幕的代码,使之成为现实。但是,当我运行完启动屏幕的代码并使titleScreen=true时,titleScreen的条件分支就不起作用了!请帮帮我:(我已经做了大量的测试,但无法找出错误所在

这是我的密码:

titleScreen = false
local logo = display.newImage("logo.png", 155, 275)
logo.alpha = 0

local function makeMenuTrue()
    logo:removeSelf()
    print("menu should be TRUE")
    print("WHY DOESN'T IT WORK")
    titleScreen = true  
end

local function fadeOut()
    transition.to(logo, {time = 2000, alpha = 0, onComplete = makeMenuTrue})
end

transition.to(logo, {time = 2000, alpha = 1, onComplete = fadeOut})

if (titleScreen == true) then
    print("NOW IT'S TRUE")
    local mainTheme = audio.loadSound ("mainTheme.wav")
    audio.play(mainTheme)

    display.setStatusBar(display.HiddenStatusBar)
    local background1 = display.newImage("Title.png", 155, 275)

    local flare = display.newImage("flare2.png", 40, 30)
    flare.xScale = .5
    flare.yScale = .5
    local flare2 = display.newImage("flare2.png", 400, 70)
    flare2.xScale = .6
    flare2.yScale = .6
    local flare3 = display.newImage("flare2.png", 400, 70)
    flare2.xScale = .4
    flare2.yScale = .4

    local function moveFlare1() 
        transition.to(flare, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare1})
    end
    local function moveFlare2()
        transition.to(flare2, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare2})
    end 
    local function moveFlare3()
        transition.to(flare3, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare3})
    end 

    transition.to(flare, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare1})
    transition.to(flare2, {time=2500, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare2}) 
    transition.to(flare3, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare3}) 

--[[local textbox = display.newRect(160, 214, 320, 90)
textbox:setFillColor(.1, .1, .1, .6)
]]

    local textbox = display.newImage("MessageBack2.png", 155, 254, 320, 90)
    textbox.yScale = .7
    local textline = display.newImage("flare2.png", 0, 228)
    textline.xScale = 20
    textline.yScale = .3
    local textOption = 1

    local prompt1 = display.newText ("Start Game", 155, 230, "Goudy Old Style", 20)
    local prompt2 = display.newText ("Continue", 155, 255, "Goudy Old Style", 20)
    local prompt3 = display.newText ("Exit Game", 155, 280, "Goudy Old Style", 20)
    prompt1:setFillColor(0,0,0)

    local function textShrink()
        transition.to(textline, {time = 150, yScale = .3})
    end

    local function moveTextBox()
        if (textOption == 1) then
            textOption = 2 
            transition.to(textline, {time = 200, y = 253, onComplete = textShrink})
            prompt2:setFillColor(0,0,0)
            prompt1:setFillColor(1,1,1)
        else 
            if (textOption == 2) then
                textOption = 3
                transition.to(textline, {time = 200, y = 278, onComplete = textShrink})
                prompt3:setFillColor(0,0,0)
                prompt2:setFillColor(1,1,1)
            else 
                if (textOption == 3) then 
                    textOption = 1
                    transition.to(textline, {time = 200, y = 228, onComplete = textShrink})
                    prompt1:setFillColor(0,0,0)
                    prompt3:setFillColor(1,1,1)
                end
            end
        end
    end

    local function expandText()
        if (textOption == 1 ) then
            print("object 1 has been tapped????")
        else 
            if (textOption == 2) then
                print("object 2 has been tapped????")
            else 
                if (textOption == 3) then
                    print("object 3 has been tapped????")
                end
            end
        end
    end

    transition.to(textline, {time = 150, yScale = .5, 0, onComplete = moveTextBox})
    textbox:addEventListener("tap", expandText)
    else 
        print("lmao no it's not true")
    end

非常感谢您的帮助!!

在将标题屏幕值设置为false后,对标题屏幕值作出反应的代码将完全运行。如果语句不在某个单独的函数中,则该语句仅在加载并运行此脚本时调用,而makeMenuTrue()甚至没有调用一次

但是当/如果您再次加载/运行它时,您已经无条件地执行了
titleScreen=false
,因此您不能让
titleScreen
存储任何可以通过先前的
makeMenuTrue
调用更新的信息


拆分它。使
titleScreen
条件检查一个单独的函数,该函数在不重新加载脚本的情况下调用。或者至少检查
titleScreen
变量是否已经存在,然后在脚本重新加载时将其强制为
false

您可以简单地使用函数(
showttitleScreen
)而不是变量(
titleScreen
)。我将
if-else
语句中的所有代码放在函数体中

local logo = display.newImage("logo.png", 155, 275)
logo.alpha = 0

local function makeMenuTrue()
    logo:removeSelf()
    print("menu should be TRUE")
    print("WHY DOESN'T IT WORK")
    showTitleScreen()
end

local function fadeOut()
    transition.to(logo, {time = 2000, alpha = 0, onComplete = makeMenuTrue})
end

transition.to(logo, {time = 2000, alpha = 1, onComplete = fadeOut})

local function showTitleScreen()
    print("NOW IT'S TRUE")
    local mainTheme = audio.loadSound ("mainTheme.wav")
    audio.play(mainTheme)

    display.setStatusBar(display.HiddenStatusBar)
    local background1 = display.newImage("Title.png", 155, 275)

    local flare = display.newImage("flare2.png", 40, 30)
    flare.xScale = .5
    flare.yScale = .5
    local flare2 = display.newImage("flare2.png", 400, 70)
    flare2.xScale = .6
    flare2.yScale = .6
    local flare3 = display.newImage("flare2.png", 400, 70)
    flare2.xScale = .4
    flare2.yScale = .4

    local function moveFlare1() 
        transition.to(flare, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare1})
    end
    local function moveFlare2()
        transition.to(flare2, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare2})
    end 
    local function moveFlare3()
        transition.to(flare3, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare3})
    end 

    transition.to(flare, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare1})
    transition.to(flare2, {time=2500, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare2}) 
    transition.to(flare3, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare3}) 

--[[local textbox = display.newRect(160, 214, 320, 90)
textbox:setFillColor(.1, .1, .1, .6)
]]

    local textbox = display.newImage("MessageBack2.png", 155, 254, 320, 90)
    textbox.yScale = .7
    local textline = display.newImage("flare2.png", 0, 228)
    textline.xScale = 20
    textline.yScale = .3
    local textOption = 1

    local prompt1 = display.newText ("Start Game", 155, 230, "Goudy Old Style", 20)
    local prompt2 = display.newText ("Continue", 155, 255, "Goudy Old Style", 20)
    local prompt3 = display.newText ("Exit Game", 155, 280, "Goudy Old Style", 20)
    prompt1:setFillColor(0,0,0)

    local function textShrink()
        transition.to(textline, {time = 150, yScale = .3})
    end

    local function moveTextBox()
        if (textOption == 1) then
            textOption = 2 
            transition.to(textline, {time = 200, y = 253, onComplete = textShrink})
            prompt2:setFillColor(0,0,0)
            prompt1:setFillColor(1,1,1)
        else 
            if (textOption == 2) then
                textOption = 3
                transition.to(textline, {time = 200, y = 278, onComplete = textShrink})
                prompt3:setFillColor(0,0,0)
                prompt2:setFillColor(1,1,1)
            else 
                if (textOption == 3) then 
                    textOption = 1
                    transition.to(textline, {time = 200, y = 228, onComplete = textShrink})
                    prompt1:setFillColor(0,0,0)
                    prompt3:setFillColor(1,1,1)
                end
            end
        end
    end

    local function expandText()
        if (textOption == 1 ) then
            print("object 1 has been tapped????")
        else 
            if (textOption == 2) then
                print("object 2 has been tapped????")
            else 
                if (textOption == 3) then
                    print("object 3 has been tapped????")
                end
            end
        end
    end

    transition.to(textline, {time = 150, yScale = .5, 0, onComplete = moveTextBox})
    textbox:addEventListener("tap", expandText)
end 

transition.to()
在脚本运行时被调用,在完成时调用
fadeOut()
,然后在完成时调用
makeMenuTrue()
call,然后
titleScreen
将在脚本的最开始处被删除。哇,谢谢!我确实在几个转换后使用“onComplete”调用了“makeMenuTrue”,但我知道为什么会这样。我可以在脚本上要求更多的规范吗“当/如果您再次加载/运行它,您将无条件地执行titleScreen=false,因此您不能让titleScreen存储任何可由上一次makeMenuTrue调用更新的信息“虽然是部分?我不太明白--作为false执行的值不应该更新为makeMenuTrue调用吗?是什么阻止了这一点?如果我不太明白,很抱歉,有时这个论坛的语言对我来说相当复杂:)对标题屏幕作出反应的代码在同一块中运行,您将此变量设置为false。即如果将在淡出()之前检查标题屏幕的值调用了,或者在脚本的第一行中将
titleScreen
设置为
false
之后。无论哪种方式,
if
transition.to()
之后的语句都不会在该变量中看到
true