chris_techno25
chris_techno25

Reputation: 2487

Right Code Arrangement of Functions in Corona LUA

Hi :) I just got into Corona programming and I'm enjoying it so far. However, I have encountered problems. I'm a .NET programmer and I understand the fact that unlike .NET, code arrangement is very important in Corona. In .NET, wherever I place my function, I can use that function anywhere. In Corona, a good example is the button touch event that must be placed above the button declaration for it to work. I have this code so far. My app doesn't make any sense for now because I'm just experimenting to get this feature to work.

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
 }

So basically, when I click the start button. The question "What is your name?" should pop out and the button labels will have generated names. My code doesn't work because my function generateQuestion() is right above the button declarations. However, if I moved my function below the button declarations, then the button touch event function wouldn't work because it contains the generateQuestion() function. I cannot move the button touch function anywhere below the button declarations because that would make the button touch function useless. How do I solve this problem? Thank you very very much :)

Upvotes: 1

Views: 121

Answers (1)

catwell
catwell

Reputation: 7020

Here is a simplified example of your problem:

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

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

f()

This won't work because g is not declared in 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 ?

To solve it, predeclare g before f:

local g

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

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

f()

In your case the simplest way is to predeclare the buttons. Add this somewhere before generateQuestion():

local button1, button2, button3, button4

and remove the locals where you set them, i.e. this:

local button1 = widget.newButton{ ... }

becomes this:

button1 = widget.newButton{ ... }

Upvotes: 5

Related Questions