电晕,使用计时器在循环中创建项目,需要内部计时器

时间:2014-07-31 10:03:04

标签: android loops timer lua corona

所以我目前正在为app开发提供一个机会。我已经在电晕中编程了将近2周,并且已经击中了我的第一个正确的路障。我的问题是我创造了一个项目(称为点球(它是一个你需要点击的小圆球))。每2秒调用一次点球,并给出随机坐标和用于处理点击事件的事件循环。我的问题是,我想这样做,以便在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

在介绍入门被调用之后它每2秒生成一个球(如(2000,spawnball,-1)所示。现在我需要一种方法在实际的类中添加一个计时器!!如果你能帮忙的话,我将非常感激。

1 个答案:

答案 0 :(得分:2)

您可以在产卵功能中创建一个计时器:

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

我不知道Corna,但假设计时器的第3个arg是重复的次数,这应该在它创建后执行4000ms并摧毁球。