Function Corona LUA中函数的正确代码排列

Function Corona LUA中函数的正确代码排列,function,mobile,lua,coronasdk,Function,Mobile,Lua,Coronasdk,嗨:)我刚进入科罗纳编程,到目前为止我很喜欢它。然而,我遇到了一些问题。我是一名.NET程序员,我明白与.NET不同的是,代码排列在Corona中非常重要。在.NET中,无论我将函数放在哪里,我都可以在任何地方使用该函数。在Corona中,一个很好的例子是按钮触摸事件,它必须放置在按钮声明上方才能工作。到目前为止,我有这个代码。我的应用现在没有任何意义,因为我只是在尝试让这个功能发挥作用 local widget = require( "widget" ) display.setDefault

嗨:)我刚进入科罗纳编程,到目前为止我很喜欢它。然而,我遇到了一些问题。我是一名.NET程序员,我明白与.NET不同的是,代码排列在Corona中非常重要。在.NET中,无论我将函数放在哪里,我都可以在任何地方使用该函数。在Corona中,一个很好的例子是按钮触摸事件,它必须放置在按钮声明上方才能工作。到目前为止,我有这个代码。我的应用现在没有任何意义,因为我只是在尝试让这个功能发挥作用

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()
函数。我不能将按钮触摸功能移动到按钮声明下方的任何位置,因为这将使按钮触摸功能无效。我如何解决这个问题?非常感谢:)

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

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 ?
local g

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

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

f()
要解决此问题,请在
f
之前预先删除
g

$ 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 ?
local g

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

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

f()
在您的情况下,最简单的方法是预先取消按钮。将此添加到
generateQuestion()之前的某个位置。

并移除设置它们的
本地
s,即:

local button1 = widget.newButton{ ... }
变成这样:

button1 = widget.newButton{ ... }

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

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 ?
local g

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

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

f()
要解决此问题,请在
f
之前预先删除
g

$ 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 ?
local g

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

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

f()
在您的情况下,最简单的方法是预先取消按钮。将此添加到
generateQuestion()之前的某个位置。

并移除设置它们的
本地
s,即:

local button1 = widget.newButton{ ... }
变成这样:

button1 = widget.newButton{ ... }

我会投赞成票,因为这不仅回答了我的问题,这将使我避免将来遇到的所有安排问题,我真傻,没有想到这个解决方案。我会投赞成票,因为这不仅回答了我的问题,这将使我避免将来遇到的所有安排问题,我真傻,竟然不想到这个解决办法。