2017-01-31 15 views
0

我加入這個社區是因爲它看起來很棒,而且我是一個新的編碼器,所以我想通過遵循這樣一個好的社區的建議來嘗試和快速改進。 :)Lua中的條件分支不是沒有明確的理由嗎?

我有一個關於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 

任何幫助深表感謝!

回答

1
titleScreen值反應

代碼運行設置titleScreen爲false準確後,即if語句是不是在某些單獨的功能,所以它被稱爲只有當你加載和運行此腳本,當makeMenuTrue()不叫連一次。

但是,如果您再次加載/運行它,則您無條件執行titleScreen = false,因此您不能讓titleScreen存儲可以通過之前的makeMenuTrue調用更新的任何信息。

拆分它。使titleScreen條件檢查一個單獨的函數,該函數在不重新加載腳本的情況下被調用。或者至少檢查titleScreen變量是否已經存在,然後在腳本重新加載時將其強制爲false

+0

'腳本運行時調用'transition.to()',完成時調用'fadeOut()',完成時調用'makeMenuTrue()'。 –

+0

'makeMenuTrue'將被調用。但是'if'語句在腳本重載之前不會被調用,並且如果它將在'fadeout()'調用後重新加載,那麼'titleScreen'將在腳本的開始處被擦除。 – Vlad

+0

哇,謝謝!我在使用「onComplete」進行一對夫妻轉換後確實調用了「makeMenuTrue」,但我明白了爲什麼這是真的。我可以要求更多關於「何時/如果你再次加載/運行它,你有無條件地執行titleScreen = false的規範,所以你不能讓titleScreen存儲任何可以被以前的makeMenuTrue調用更新的信息」部分?我不太明白 - 不應該將執行爲false的值更新爲makeMenuTrue調用?什麼阻止?對不起,如果我不明白,有時這個論壇的語言對我來說很複雜:) –

0

您可以簡單地使用函數(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 
相關問題