簡體   English   中英

將 addEventListener 方法附加到創建的對象時獲取“nil”值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM