Oop 获得';无';将addEventListener方法附加到创建的对象时的值

Oop 获得';无';将addEventListener方法附加到创建的对象时的值,oop,lua,coronasdk,Oop,Lua,Coronasdk,我刚开始用电晕弄湿我的脚,试着用OOP。这只是一个简单的配对游戏,可以帮助我像OOP一样思考。我有两个类,一个类将创建一张卡的实例(我将创建这种类型的卡类的多个对象),另一个是MatchCardsManager类-这将创建卡并应用属性 我得到的错误是,在创建对象“MatchCard”后,我试图对该对象应用“addEventListener”。但当我这样做时,我收到以下错误 Support/Outlaw/Sandbox/5/MatchCardsManager.lua:53: attem

我刚开始用电晕弄湿我的脚,试着用OOP。这只是一个简单的配对游戏,可以帮助我像OOP一样思考。我有两个类,一个类将创建一张卡的实例(我将创建这种类型的卡类的多个对象),另一个是MatchCardsManager类-这将创建卡并应用属性

我得到的错误是,在创建对象“MatchCard”后,我试图对该对象应用“addEventListener”。但当我这样做时,我收到以下错误

     Support/Outlaw/Sandbox/5/MatchCardsManager.lua:53: 
attempt to call method 'addEventListener' (a nil value)
        stack traceback:
如果我在addEventListener上注释掉这些信息,所有对象都会根据我在MatchCard类中创建的构造函数显示

下面是我的文件-我得到的错误在MatchCardsManager类中

mCard[i] = MatchCardsManager:displayPlacementCard(i, temp, x, y)
mCard[i]:addEventListener("touch", myTouch)
如果您有任何关于修复此方法或更好方法的帮助或建议,我们将不胜感激。多谢各位

MatchCard类目前只是一个简单的构造函数,因为这不是我的问题

-- MatchCard Class
-- Meta Data
local sceneGroup = sceneGroup

local MatchCard = { }
local MatchCard_mt = { __index = MatchCard } -- metatable

------------------------------------------------
-- PRIVATE FUNCTION 
------------------------------------------------

------------------------------------------------
-- PUBLIC FUNCTION
------------------------------------------------
-- constructor
function MatchCard.new (id, animal, state, imageId, x, y)

    local newMCard = display.newRoundedRect( x, y, 59, 47, 5)
    newMCard.strokeWidth = 3
    newMCard:setFillColor( 0.5 )
    newMCard:setStrokeColor( 1, 0, 0 )
    newMCard.properties = {
        id = id or nil,
        animal = animal or nil,
        state = state or 0, 
        imageId = imageId,
    }

    return setmetatable ( newMCard, MatchCard_mt )

end
MatchCardsManager类在那里,我计划创建许多卡片实例

-- require files
local MatchCard = require('MatchCard') --MatchCard

local sceneGroup = sceneGroup
local MatchCardsManager = {} -- originally we should use a displayGroup

-- TODO: insert group into scene

local animalPicsReference = { "dog", "dog", "cat", "cat", "pig", "pig" , "fish", "fish"} 

-- manager class properties
MatchCardsManager.totalCards = 8
MatchCardsManager.totalPairs = 4
MatchCardsManager.pairsFound = 0
MatchCardsManager.firstCardSelected = 0
MatchCardsManager.secondCardSelected = 0

-- lets create 6 MatchCardFiles
function MatchCardsManager:create()

    local animalPics = animalPicsReference
    local x = 108 - 85
    local y = 125
    print("do we go here never works")

    local mCard = {}

    for i=1, 4  
        do 
           x = x + 85 
           num = math.random(1, #animalPics)
           temp = animalPics[num]
           table.remove(animalPics, num) 
           mCard[i] = MatchCardsManager:displayPlacementCard(i, temp, x, y)
           mCard[i]:addEventListener("touch", myTouch)

    end

    x = 108 - 85
    y = 195 
    for j = 5, 8 do 
            x = x + 85          
           num = math.random(1, #animalPics)
           temp = animalPics[num]
           table.remove(animalPics, num) 
           mCard[j] = MatchCardsManager:displayPlacementCard(j, temp, x, y)
           print(type(mCard[j]))
           mCard[j]:addEventListener("touch", myTouch)
    end
    --mCards:addEventListener("touch", myTouch)
    return mCard
end

local function  myTouch( event )
    if event.phase == "began" then
            print( "You touched the object! "..event.target.imageId)
    end
end

function MatchCardsManager:displayPlacementCard(idx, animal, x, y)
    -- randomly place the cards in the object id

    local card = MatchCard.new(idx, animal, 0, animal, x, y)
    --card:show(x,y) -- displays card and that is it
    print("animal added is "..card.properties.imageId)
    return card
end

return MatchCardsManager

问题出在构造函数中。
localnewmcard=display.newRoundedRect(…)
创建显示对象,但线条:
return setmetatable(newMCard,MatchCard\u mt)
覆盖
显示
对象所拥有的元表,因此它不再有权访问用于查找
addEventListener
的display的
索引
元方法

要解决这个问题,您需要研究如何在Lua中添加继承。
请参见,以便可以继承
addEventListener
。解决方案如下:
setmetatable(MatchCard,{{uuu index=ShapeObject})
=display.ShapeObject}
——我不确定Corona是如何实现其类的。

thanx rpattiso,我注意到我正在尝试在表中设置一个显示对象,而它应该在displayGroup中,或者包装在表对象中的属性中。看看我能想出什么