Corona LUA中函数的正确代码安排

时间:2014-09-20 09:57:43

标签: function mobile lua corona

嗨:)我刚进入Corona编程,到目前为止我很享受。但是,我遇到了问题。我是一名.NET程序员,我理解这一事实,与.NET不同,代码安排在Corona中非常重要。在.NET中,无论我将函数放在哪里,我都可以在任何地方使用该函数。在Corona中,一个很好的例子是按钮触摸事件,必须放在按钮声明上方才能工作。到目前为止我有这个代码。我的应用程序现在没有任何意义,因为我只是在试验让这个功能起作用。

enter image description here

local widget = require( "widget" )

display.setDefault( "background", 1, 1, 1 )
local controlwidth = display.contentWidth-20
local controlheight = display.contentHeight/10


local questiontextproperties = {
   text = "",    
   x = display.contentCenterX,
   y = (display.contentHeight/10)*2,
   fontSize = 30,
   width = display.contentWidth,
   height = controlheight,
   align = "center"
}

local questiontext = display.newText( questiontextproperties )
questiontext:setFillColor( 0, 0, 0 )
questiontext.text = ""

local function generateQuestion()
    question="What is your name?"
    button2:setLabel("Chris") 
    button3:setLabel("John") 
    button4:setLabel("Steph") 
    return question
end

--Functions
local function buttonTouch(event)
    --take in the event and get the associated display object
    local myButtons = event.target
    --now find out which button it is
    local nameString = myButtons.id

    --use began and ended phases to change text based on touches
    if event.phase == "began" then
        --set the label text to the id
        if nameString == "button1" then
            questiontext.text = generateQuestion()
        end
    elseif event.phase == "ended" then
        --back to default
        --label.text = "No Button Touch"
    end
    return true
end

local button1 = widget.newButton{
    id = "button1", -- id is unique and essential when using event handlers (see addEventListener below)
    label = "Start",
    emboss = false,
    shape="roundedRect",
    left = 10,
    top = (display.contentHeight/10)*6,
    width = controlwidth,
    height = controlheight,
    cornerRadius = 2,
    fillColor = { default={ 1, 0.2, 0.5, 0.7 }, over={ 1, 0.2, 0.5, 1 } },
    --strokeColor = { default={ 1, 0.4, 0, 1 }, over={ 0.8, 0.8, 1, 1 } },
    strokeWidth = 4,
    onEvent = buttonTouch
}

local button2 = widget.newButton{
    id = "button2", -- id is unique and essential when using event handlers (see addEventListener below)
    label = "",
    emboss = false,
    shape="roundedRect",
    left = 10,
    top = ((display.contentHeight/10)*7)+10,
    width = controlwidth,
    height = controlheight,
    cornerRadius = 2,
    fillColor = { default={ 1, 0.2, 0.5, 0.7 }, over={ 1, 0.2, 0.5, 1 } },
    --strokeColor = { default={ 1, 0.4, 0, 1 }, over={ 0.8, 0.8, 1, 1 } },
    strokeWidth = 4,
    onEvent = buttonTouch
}

local button3 = widget.newButton{
    id = "button3", -- id is unique and essential when using event handlers (see addEventListener below)
    label = "",
    emboss = false,
    shape="roundedRect",
    left = 10,
    top = ((display.contentHeight/10)*8)+20,
    width = controlwidth,
    height = controlheight,
    cornerRadius = 2,
    fillColor = { default={ 1, 0.2, 0.5, 0.7 }, over={ 1, 0.2, 0.5, 1 } },
    --strokeColor = { default={ 1, 0.4, 0, 1 }, over={ 0.8, 0.8, 1, 1 } },
    strokeWidth = 4,
    onEvent = buttonTouch
}

local button4 = widget.newButton{
    id = "button4", -- id is unique and essential when using event handlers (see addEventListener below)
    label = "",
    emboss = false,
    shape="roundedRect",
    left = 10,
    top = ((display.contentHeight/10)*9)+30,
    width = controlwidth,
    height = controlheight,
    cornerRadius = 2,
    fillColor = { default={ 1, 0.2, 0.5, 0.7 }, over={ 1, 0.2, 0.5, 1 } },
    --strokeColor = { default={ 1, 0.4, 0, 1 }, over={ 0.8, 0.8, 1, 1 } },
    strokeWidth = 4,
    onEvent = buttonTouch
 }

所以基本上,当我点击开始按钮时。问题是“你叫什么名字?”应弹出,按钮标签将生成名称。我的代码不起作用,因为我的函数generateQuestion()正好在按钮声明之上。但是,如果我将我的函数移到按钮声明下面,那么按钮触摸事件函数将无法工作,因为它包含generateQuestion()函数。我无法在按钮声明下方的任何位置移动按钮触摸功能,因为这会使按钮触摸功能无用。我该如何解决这个问题?非常感谢你:))

1 个答案:

答案 0 :(得分:5)

以下是您的问题的简化示例:

local function f()
    print "f"
    return g()
end

local function g()
    print "g"
    return f()
end

f()

这不起作用,因为g中未声明f

$ lua xx.lua 
f
lua: xx.lua:3: attempt to call global 'g' (a nil value)
stack traceback:
    xx.lua:3: in function 'f'
    xx.lua:11: in main chunk
    [C]: in ?

要解决此问题,请在g之前预先f

local g

local function f()
    print "f"
    return g()
end

g = function()
    print "g"
    return f()
end

f()

在您的情况下,最简单的方法是预先按下按钮。在generateQuestion()之前的某处添加:

local button1, button2, button3, button4

并删除您设置它们的local,即:

local button1 = widget.newButton{ ... }

成为这个:

button1 = widget.newButton{ ... }