user3895076
user3895076

Reputation: 11

Corona, creating an item in a loop using a timer, that needs an internal timer

so im currently giving app development a shot. Ive been programming in corona for nearly 2 weeks and have hit my first proper road block. My problem is that ive created an item ( called a pointball ( its a little circle ball that you need to tap )). the pointball is called every 2 seconds and is given random coords and an event loop for handling a tap event. My problem, is that i would like to make it so that after 4 seconds each pointball disappears, which i currently cannot do because if i do the timer inside the item it gets called once and the timer isn't called for long enough. If its outside it throws up an error as it cannot see the contents inside the class. Here is the code ( i know its messy, and i know its inefficient, please focus on the problem and not my horrible code style )

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

after the intro starter gets called and it spawns a ball every 2 seconds ( as seen with the ( 2000, spawnball, -1 ). Now i need a way to add a timer inside the actual class!! if you can help, i will be so grateful.

Upvotes: 1

Views: 199

Answers (1)

Advert
Advert

Reputation: 653

You can create a timer inside your spawning function:

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

I don't know Corna, but assuming the 3rd arg for the timer is the number of repeats, this should execute 4000ms after it's created, and destroy the ball.

Upvotes: 2

Related Questions