在电晕中向表对象添加事件侦听器

时间:2016-06-17 22:19:46

标签: lua sdk corona lua-table

local function removeTrumps(obj)
    obj:removeSelf()

    trumps=trumps-1
 --[[local thisTrump=myTrump.id
    display.remove(myTrump[thisTrump])
    myTrump[thisTrump]=nil
    table.remove(myTrump,thisTrump)]]--

    if (timeLeft~=false) then 
        if(trumps==0) then
            timer.cancel(gameTimer)
            gameOver("winner")

        elseif(trumps<=40) then

            gameOver("notbad")
        elseif(trumps>=31) then

            gameOver("loser")
        end

    end
end


local function startGame()
myTrump[#myTrump+1]=display.newImageRect("tp.png",25,25)
myTrump[#myTrump].x=Random(50,_W-50)
myTrump[#myTrump].y=(_H+10)
myTrump[#myTrump].id=#myTrump
physics.addBody(myTrump[#myTrump],"dynamic", {density=.1, friction=0, bounce=.9, radius=9})


 --TOUCH FUNCTION FIX IT
   function onTouch(event)

    if(timeLeft~=false) then

        if (playerReady==true) then
            if(event.phase=="ended") then
            removeTrumps(self)


        end
    end
end
end
--if i put onTouch then removetrumps is ? if i leave it as #myTrump it just doenst recognize the touch
myTrump[#myTrump]:addEventListener("touch", onTouch)
trumps=trumps+1

if(trumps==numTrumps) then
    gameTimer=timer.performWithDelay(1000,countDown,totalTime)
else

    playerReady=false
end
end

所以这是我的问题。我不知道如何正确地将onTouch事件监听器设置为表格对象#myTrump,并且我试图在您点击图像时将其删除,但它不起作用对。它现在的方式是谈论obj(在removeTrumps函数中)是一个零值。我该如何解决?物品不会消失。

1 个答案:

答案 0 :(得分:0)

尝试为removeTrumps(self)更改此removeTrumps(event.target)。另外,您无需将boolean与值truefalse进行比较:

if timeLeft then --if (timeLeft ~= false) then "not false is true"
    if playerReady then --if (playerReady == true) then
        --some code here
    end
end
相关问题