我刚开始用Corona弄湿脚,试着和OOP一起工作。这只是一个简单的比赛制作游戏,帮助我像OOP一样思考。我有2个类,一个类将创建一个卡的实例(我将创建这种类型的卡类的多个对象),另一个类是MatchCardsManager类 - 这将创建卡并应用属性
我得到的错误是,在我创建了对象" MatchCard"我试图应用" addEventListener"对象。但当我这样做时,我收到以下错误
Support/Outlaw/Sandbox/5/MatchCardsManager.lua:53:
attempt to call method 'addEventListener' (a nil value)
stack traceback:
如果我在addEventListener上注释掉信息,则所有对象都会相应地显示在我在MatchCard Class中创建的构造函数。
以下是我的文件 - 我得到的错误是在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 Class在那里我计划创建许多卡片实例
-- 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
答案 0 :(得分:1)
问题出在构造函数中。
local newMCard = display.newRoundedRect(...)
创建一个显示对象,但行:
return setmetatable(newMCard, MatchCard_mt)
覆盖display
对象所具有的元表,因此它不再能够访问用于查找__index
的显示addEventListener
元方法。
要解决此问题,您需要了解如何在Lua中添加继承。
请参阅Inheritance Tutorial,以便继承addEventListener
。解决方案类似于:setmetatable(MatchCard, {__index=ShapeObject})
或=display.ShapeObject}
---我无法确定Corona如何实现其类。