Lua 中的条件分支无缘无故不起作用?
Conditional Branch in Lua isn't working for no clear reason?
我加入这个社区是因为它看起来很棒,而且我是一名新的编码员,所以我想按照这样一个不错的社区的建议尝试并快速改进。 :)
我对 lua 中的编码有疑问 -- 现在,我正在使用 Corona SDK 和 Sublime Text 3 来编写移动启动画面和标题画面的代码。我从 titleScreen = false 开始,这样我就可以 运行 启动画面的代码,使其变为 true。但是,当我完成 运行 启动画面的代码并使 titleScreen = true 时,标题画面的条件分支不起作用!请帮助我 :( 我做了很多测试,但无法找出我的错误所在。
这是我的代码:
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
非常感谢任何帮助!!
在您将 titleScreen
设置为 false 后对 titleScreen
值 运行 做出反应的代码。该 if
语句不在某个单独的函数中,因此仅当您加载 运行 此脚本时才会调用它,而 makeMenuTrue()
甚至一次都没有被调用。
但是 when/if 你又 load/run 它,你 titleScreen = false
无条件执行,所以你不能 titleScreen
存储任何可以被之前更新的信息makeMenuTrue
呼唤。
分开吧。使 titleScreen
条件检查成为一个单独的函数,无需重新加载脚本即可调用。或者至少检查 titleScreen
变量是否已经存在,然后在重新加载脚本时将其强制为 false
。
您可以简单地使用函数 (showTitleScreen
) 而不是变量 (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
我加入这个社区是因为它看起来很棒,而且我是一名新的编码员,所以我想按照这样一个不错的社区的建议尝试并快速改进。 :)
我对 lua 中的编码有疑问 -- 现在,我正在使用 Corona SDK 和 Sublime Text 3 来编写移动启动画面和标题画面的代码。我从 titleScreen = false 开始,这样我就可以 运行 启动画面的代码,使其变为 true。但是,当我完成 运行 启动画面的代码并使 titleScreen = true 时,标题画面的条件分支不起作用!请帮助我 :( 我做了很多测试,但无法找出我的错误所在。
这是我的代码:
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
非常感谢任何帮助!!
在您将 titleScreen
设置为 false 后对 titleScreen
值 运行 做出反应的代码。该 if
语句不在某个单独的函数中,因此仅当您加载 运行 此脚本时才会调用它,而 makeMenuTrue()
甚至一次都没有被调用。
但是 when/if 你又 load/run 它,你 titleScreen = false
无条件执行,所以你不能 titleScreen
存储任何可以被之前更新的信息makeMenuTrue
呼唤。
分开吧。使 titleScreen
条件检查成为一个单独的函数,无需重新加载脚本即可调用。或者至少检查 titleScreen
变量是否已经存在,然后在重新加载脚本时将其强制为 false
。
您可以简单地使用函数 (showTitleScreen
) 而不是变量 (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