0

Corona 新手(来自 Actionscript)并尝试通过一些 OOP 教程为我的应用程序创建可重用模块。目前正在研究一个数字选择器,它似乎正在工作,但当我按下按钮时也会引发错误。这是“numberPicker.lua”的代码:

local numberPicker = {}
local numberPicker_mt = { __index = numberPicker }  -- metatable
widget = require "widget"

-------------------------------------------------
-- PUBLIC FUNCTIONS
-------------------------------------------------

function numberPicker.new() -- constructor

local newNP = display.newGroup()
local value = 0

local function dec()
    print("dec")
    if value > 0 then
        value = value - 1
        valueText.text = value
    end
end

local function inc()
    print("inc")
    if value < 100 then
        value = value + 1
        valueText.text = value
    end
end

decrement = widget.newButton{
    default="gfx/dec_normal.png",
    over="gfx/dec_press.png",
    width=58, height=58,
    onRelease = dec
}

valueText = display.newText(value, 70, 10, native.systemFont, 40)
valueText:setTextColor(0, 0, 0)

increment = widget.newButton{
    default="gfx/inc_normal.png",
    over="gfx/inc_press.png",
    width=58, height=58,
    onRelease = inc
}

increment.x = 140

newNP:insert(decrement.view)
newNP:insert(valueText)
newNP:insert(increment.view)
return setmetatable( newNP, numberPicker_mt )
end

return numberPicker

用法:

local numberPicker = require( "numberPicker" )
local lengthPicker = numberPicker.new()
scrollView:insert(lengthPicker)

当我按下任一按钮时,我会打印两次以下错误:

Runtime error
attempt to call a nil value
stack traceback:
[C]: ?

任何线索任何人?谢谢,艾玛。

4

1 回答 1

0

“scrollView”变量未初始化。

于 2012-11-23T19:08:50.120 回答