![](/img/trans.png)
[英]attempt to call method 'print' (a nil value) when implementation OOP in Lua
[英]Getting a 'nil' value when attaching an addEventListener method to a created Object
我刚开始接触 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 类中创建的构造函数显示。
下面是我的文件 - 我得到的错误是在 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
问题出在构造函数中。 local newMCard = display.newRoundedRect(...)
创建一个显示对象,但这一行: return setmetatable(newMCard, MatchCard_mt)
覆盖了display
对象拥有的元表,因此它不再可以访问用于显示的__index
元方法找到addEventListener
。
要解决这个问题,您需要研究如何在 Lua 中添加继承。 请参阅继承教程,以便您可以继承addEventListener
。 解决方案将类似于: setmetatable(MatchCard, {__index=ShapeObject})
或=display.ShapeObject}
---我无法确定 Corona 如何实现其类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.