Lua的有条件分支没有明确的原因吗?

时间:2017-01-31 08:01:04

标签: lua sublimetext3 corona

我加入了这个社区,因为它看起来很棒,而且我是一个新的程序员,所以我想通过遵循这样一个好社区的建议,努力并迅速改进。 :)

我有一个关于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

非常感谢任何帮助!!

2 个答案:

答案 0 :(得分:1)

titleScreen值做出反应的代码在将titleScreen设置为false后完全运行。 if语句不在某个单独的函数中,因此只有在加载并运行此脚本时才会调用它,而makeMenuTrue()甚至不会调用一次。

但是,如果您再次加载/运行它,则无条件执行titleScreen = false,因此您无法titleScreen存储可由先前{{1}更新的任何信息打电话。

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

答案 1 :(得分:0)

您可以简单地使用函数(def num_cruncher n s = n > 20 ? -2 : -1 n.step(by: s, to: 0).entries end num_cruncher 23 #=> [23, 21, 19, 17, 15, 13, 11, 9, 7, 5, 3, 1] )而不是变量(showTitleScreen)。您放入函数体的titleScreen语句中的所有代码。

if-else