Corona,在一个循环中使用定时器创建一个需要内部定时器的项。

所以我目前正在尝试应用程序开发。我已经在 corona 中编程将近两周,遇到了第一个真正的障碍。我的问题是我创建了一个物品(称为点球(它是一个小圆球,您需要点击))。点球每两秒钟被召唤一次,并被赋予随机坐标和处理点击事件的事件循环。我的问题是,我想使每个点球在4秒后消失,但我现在无法做到这一点,因为如果我在项内部调用定时器,则只会被调用一次,定时器不够长。如果在外部,则会出现错误,因为无法查看类内部的内容。以下是代码(我知道它很混乱,也知道它效率低下,请关注问题而不是我的可怕的代码风格)

function spawnball()
        local pointball = display.newImage("pointball.png")
        pointball.x, pointball.y = math.random(30,spawnrange2),math.random(30,spawnrange)
        pointball.type = "standard"
        pointball.id = ("point")
        physics.addBody(pointball, "dynamic", {friction=0, bounce = 0})
        pointball.myName = ("destructible")
        pointball.num = 0
        pointball.plus = pointball.num + 1
        pointball.touch = function( self,event )

            if event.phase == "ended" then
                self:removeSelf()
                score1 = score1 + 1
                typenum = typenum + 1
                if typenum == 25 then
                    level = level + 1
                    typenum = 0
                end
            end
            return true
        end

        pointball:addEventListener("touch", pointball)
    end

end

function starter()
    tutorial2 = false
    timer.performWithDelay( 2000, spawnball, -1)
end

介绍后,会调用 starter 并且每两秒钟生成一颗球(如(2000、spawnball、-1)所示)。现在我需要在实际类内添加一个定时器!!如果你能帮忙,我将非常感激。

点赞
用户2698261
用户2698261

你可以在生成函数内创建一个定时器:

timer.performWithDelay(4000, function() pointball:removeSelf() end, 1)

我不知道Corna,但假设计时器的第三个参数是重复次数,这个函数应该在创建4000毫秒后执行,并销毁球。

2014-07-31 10:49:46