Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

continuous creation
Started by eltheriot Mar 24 2016 08:23 AM

- - - - -
1 reply to this topic

Best Answer roaminggamer , 24 March 2016 - 08:38 AM

Use a timer or enterFrame with a fixed period.

--
-- A dummy spawner
--
local cx,cy = display.contentCenterX,display.contentCenterY
local fw,fh = display.actualcontentWidth,display.actualContentHeight
local l = cx-fw/2
local r = cx+fw/2
local t = cy-fh/2
local b = cy+fh/2
local mRand = math.random

local function spawner()
   local enemy = display.newCircle( mRand(l,r), mRand(t,b), mRand(10,20) )
end

timer.performWithDelay( 333, spawner, -1 )

- OR - 

local n = 10
local frameCount = 0

local function enterFrame( )
   if( frameCount % n == 0 ) then
      spawner()
   end
   frameCount = frameCount + 1
end
Runtime:addEventListener("enterFrame", enterFrame)

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

eltheriot

[GLOBAL: userInfoPane.html]
eltheriot
  • Observer

  • 12 posts
  • Corona SDK

I have an object class with a function that spawns a new object, but how would I keep calling this method from main? would I need an infinite loop or maybe a do while? or should it be a recursive method? Not really sure how to implement this



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,613 posts
  • Corona SDK

  Best Answer

Use a timer or enterFrame with a fixed period.

--
-- A dummy spawner
--
local cx,cy = display.contentCenterX,display.contentCenterY
local fw,fh = display.actualcontentWidth,display.actualContentHeight
local l = cx-fw/2
local r = cx+fw/2
local t = cy-fh/2
local b = cy+fh/2
local mRand = math.random

local function spawner()
   local enemy = display.newCircle( mRand(l,r), mRand(t,b), mRand(10,20) )
end

timer.performWithDelay( 333, spawner, -1 )

- OR - 

local n = 10
local frameCount = 0

local function enterFrame( )
   if( frameCount % n == 0 ) then
      spawner()
   end
   frameCount = frameCount + 1
end
Runtime:addEventListener("enterFrame", enterFrame)



[topic_controls]
[/topic_controls]