错误的参数#-2到'插入'(代理预期,得到零)

时间:2014-08-13 07:04:37

标签: lua corona

我在这里遇到问题我不知道如何解决这个问题,本尝试任何我知道的解决方案仍然没有解决方案,请帮忙

我有2个模块

main.lua:

local comboBox = require( "dog" )

local a = display.newGroup()
local bang = {1,2,3}
local bang2 = {4,5,6}

local dog1 = comboBox.newComboBox( "Choose",100,200,100,32, "center", 32,bang)
local dog2 = comboBox.newComboBox( "Choose",200,100,100,32, "left", 32,bang2)

local b = display.newRect(0,0,10,10)

dog1:printAge()
dog2:printAge()

dog1:setTextColor(1,0,0)
dog2:setTextColor(0,1,1)

a:insert(b)
a:insert(dog1)-- error is here

外部模块dog.lua:

local comboBox = display.newGroup()
local nComboBox = display.newGroup()
local widget = require("widget")
local comboBox_mt = { __index = comboBox }  -- metatable
-- test = {}
-- local t = {}

-------------------------------------------------
-- PRIVATE FUNCTIONS
-------------------------------------------------

local function getDogYears( realYears ) -- local; only visible in this module
    -- return realYears * 7
end

--Scroll View

local function scrollListener( event )

local phase = event.phase
if ( phase == "began" ) then print( "Scroll view was touched" )
elseif ( phase == "moved" ) then print( "Scroll view was moved" )
elseif ( phase == "ended" ) then print( "Scroll view was released" )
end

-- In the event a scroll limit is reached...
if ( event.limitReached ) then
    if ( event.direction == "up" ) then print( "Reached top limit" )
    elseif ( event.direction == "down" ) then print( "Reached bottom limit" )
    elseif ( event.direction == "left" ) then print( "Reached left limit" )
    elseif ( event.direction == "right" ) then print( "Reached right limit" )
    end
end

return true
end

-- Create the scrollview widget
local function createScrollView(sx,sy,sWidth,sHeight,sList)
    -- body
    scrollView = widget.newScrollView
    {
    top = sy+sHeight/2,
    left = sx-sWidth/2,
    width = sWidth,
    height = sList*sHeight,
    scrollWidth = 0,
    scrollHeight = sList*sHeight,
    horizontalScrollDisabled = true,
    listener = scrollListener
}
-- scrollView.anchorX = 1
-- scrollView.anchorY = 0
return scrollView
end


--show drop box and its content
local function createDropDown(dx, dy, dWidth, dHeight, dList)

-- local dropRect = display.newRect(dx,dy+dHeight,dWidth,dHeight)
-- dropRect:setFillColor(1,1,0)
local newListContent = {}
Y = 0+dHeight/2

for i = 1,#dList do

    local optionsForList = {
        text = dList[i],     
        x = 0,
        y = Y,
        font = native.font,   
        fontSize = 32,
        align = "left" --new alignment parameter
    }

    newListContent[i] = display.newText(optionsForList)
    newListContent[i].anchorX = 0
    newListContent[i]:setFillColor(1,0,0)
    Y = Y + dHeight+1
    scrollView:insert(newListContent[i])
    nComboBox:insert(scrollView)
    end

end


--drop box
local function bringDropDown( self, event )
    -- body
local phase = event.phase
if phase == "began" then
    -- print(self.text)
elseif phase == "moved" then

else

    self.text = "Choose an Item"
    if self.state == "n" then
        createScrollView(self.x,self.y,self.width,self.height, #self.list)
        createDropDown(self.x,self.y,self.width,self.height, self.list)
    else

    end


end

return true

end
-------------------------------------------------
-- PUBLIC FUNCTIONS
-------------------------------------------------

--Create new ComboBox
function comboBox.newComboBox( name, cx, cy, cWidth, cHeight, cAlign, cFontSize, cList)     -- constructor

-- local autoincre = 0
-- local tContents = {}

local comboBoxTextOptions = {
    text = name,     
    x = cx,
    y = cy,
    width = cWidth,     --required for multi-line and alignment
    height = cHeight+1 ,
    font = native.font,   
    fontSize = cFontSize,
    align = cAlign --new alignment parameter
}

local comboBoxFill = display.newRect(cx,cy,cWidth,cHeight)
local tNewComboBox = {
    display.newText(comboBoxTextOptions),
    -- name = name or "Unnamed",
    -- age = ageInYears or 2
}
print(#cList)
tNewComboBox[1].touch = bringDropDown
tNewComboBox[1].name = name
tNewComboBox[1].state = "n"
tNewComboBox[1].list = cList
tNewComboBox[1]:addEventListener("touch",tNewComboBox[1])
nComboBox:insert(comboBoxFill)
nComboBox:insert(tNewComboBox[1])
return setmetatable( tNewComboBox, comboBox_mt )
end


-------------------------------------------------

function comboBox:setTextColor(cx,cy,cz)
self[1]:setFillColor(cx,cy,cz)
end

-------------------------------------------------


return comboBox, nComboBox

堆栈追溯:

File: main.lua
Line: 36
Bad argument #-2 to 'insert' (Proxy expected, got nil)

stack traceback:
[C]: in function 'insert'
main.lua:36: in main chunk

现在还在想这里要做什么。这是阻碍我的项目的唯一因素。请帮忙,谢谢:)

1 个答案:

答案 0 :(得分:-1)

您不能将metatable直接与Corona显示对象一起使用。 在这种情况下,newComboBox必须返回显示对象。