No idea how to get this code to go on timer

I’ve posted this code before and did get a response. However, I still could not follow/understand how to make this happen.

I have a modified multipuck example that I’m trying to make the objects (in this case, diamonds) appear automatically. Right now I need to press a different object (button) to trigger the diamonds to appear. and then they only appear one at a time.

What i would like to have done is to drop the need to use a button to activate the diamonds and just have them populate (spawn) the screen automatically. I have been racking my brain, tried all kinds of examples but cannot get that function to work with “THIS” code.

Any help would be appreciated.
I’m basically not looking for an example so much as the actual code to place inside my code…
display.setStatusBar( display.HiddenStatusBar )
math.randomseed(os.time())
math.random()
math.random()
local physics = require(“physics”)
local gameUI = require(“gameUI”)
local easingx = require(“easingx”)

physics.start()
physics.setGravity( 0, 9.8) ------------------------------------------------------------------------------------------9.8 earth---------0,0= no gravity in any direction

popSound = audio.loadSound (“pop2_wav.wav”)
labelFont = gameUI.newFontXP{ ios=“Zapfino”, android=native.systemFont }

system.activate( “multitouch” )

local button = display.newImage ( “space1.png”)------------------------------------------------------------------------------------------------eventlistner attached
button.x = 50
button.y = 450
local diamondGfx = { “square-yellow.png”, “square-orange.png”, “square-blue.png”, “square-red.png”, “square-aqua.png”, “square-green.png”, “square-bomb.png”,“square-white.png”,}
local alldiamonds = {} ----------------------------------------------------------------------------------------------------------- empty table for storing objects
local function removeOffscreenItems()
for i = 1, #alldiamonds do
local onediamond = alldiamonds[i]
if (onediamond and onediamond.x) then
if onediamond.x < -100 or onediamond.x > display.contentWidth + 100 or onediamond.y < -100 or onediamond.y > display.contentHeight + 100 then
onediamond:removeSelf()
table.remove( alldiamonds, i )
end
end
end
end

local function dragBody( event )
return gameUI.dragBody( event )
end
----------------------------------------------------------------------------TOP SQUARES-----------------------------------------------------SPAWNdiamond
local function spawndiamond( event )
local phase = event.phase
if “ended” == phase then
audio.play( popSound )
randImage = diamondGfx[math.random( 1, 8 )]
alldiamonds[#alldiamonds + 1] = display.newImage( randImage )
local diamond = alldiamonds[#alldiamonds]

diamond.x = 60
diamond.y = 50

----------------------------------------------------------------------------------CODE TO MOVE DIAMONDS
local diamondmove = diamond
physics.addBody( diamondmove, { density=1.5, friction=0.6, radius=00.3, bounce=0.3 } )
diamondmove.x=40
diamondmove.y=60

local targetToMove = nil
local function setTarget(event)
local target = event.target
targetToMove = target
return true
end

local function movediamondmove(event)
if targetToMove ~= nil then
targetToMove.x = event.x
targetToMove.y = event.y
targetToMove = nil
end
return true
end

diamondmove:addEventListener(“tap”, setTarget)
Runtime:addEventListener(“tap”, movediamondmove)
diamond:addEventListener( “touch”, dragBody )
end

return true
end
button:addEventListener( “touch”, spawndiamond ) – touch the screen to create diamonds

local borderBottom = display.newRect( 30, 415, 258, 1 )
borderBottom:setFillColor( 255, 255, 255 )
physics.addBody( borderBottom, “static”, borderBodyElement )
[import]uid: 127842 topic_id: 22547 reply_id: 322547[/import]

This will add an enterFrame listener called gameLoop, and every frame it will compare the current time against the time stored in spawnTimer. If it is greater than ‘interval’ (2 seconds), it will fire spawndiamond, and set spawnTimer to the current time.

[lua]local spawnTimer = system.getTimer()
local interval = 2000 – 2 seconds, could be randomised?
local gameIsActive = true

local function spawndiamond( )

audio.play( popSound )
randImage = diamondGfx[math.random( 1, 8 )]
alldiamonds[#alldiamonds + 1] = display.newImage( randImage )
local diamond = alldiamonds[#alldiamonds]
diamond.x = 60 – could be randomised?
diamond.y = 50 – could be randomised?
end

local gameLoop = function ()

if gameIsActive then – set to false when paused/end of game

local currentTime = system.getTimer()

local diff = currentTime - spawnTimer

if diff > interval then

spawnTimer = system.getTimer()
spawndiamond()

end

end
end

Runtime:addEventListener(“enterFrame”,gameLoop)[/lua]
Alternatively you could have a simple timer.performWithDelay statement, but I prefer the above as it allows you to have other timers controlled in the same code block, i.e. you could have a timer that fires every second to update the game clock, or one that fires at random intervals to spawn something else like a bonus item or something. You can also adjust ‘interval’ as the level progresses to make them appear more often, whereas with the below, you would have to cancel and start a new timer.
[lua]timer.performWithDelay(interval, spawndiamond, 0) – loop forever[/lua]

[import]uid: 93133 topic_id: 22547 reply_id: 89883[/import]

Thanks nick!
Works like a charm. [import]uid: 127842 topic_id: 22547 reply_id: 89919[/import]