Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to use a timer to move spawned objects
Started by nml2727 Oct 14 2019 09:39 AM

- - - - -
4 replies to this topic
timer spawning objects timer.performwithdelay
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

nml2727

[GLOBAL: userInfoPane.html]
nml2727
  • Enthusiast

  • 96 posts
  • Corona SDK

I am having trouble understanding the proper way to use timers when spawning multiple objects that each need a timer to run some function(s).

 

I am not sure the proper place to put the timer code and how to correctly call the function for each particular object.  

 

For example, I am spawning some number of enemies with code like this and moving them to new locations:

local enemies = {}

local function move(self)
 local newX, newY = getMoveLocation(self)
 transition.to(self, {time = 500, x=newX, y=newY})
end

for i = 1, num_enemies do
 local enemy = display.newSprite(sheet, sequences)
 enemy.x, enemy.y = getSpawnLocation()
 enemy.move = move
 enemy:move()
 enemies[i] = enemy
 display_group:insert(enemy)
end

I would like to have a timer which is called every so often which moves each enemy.  I am having trouble knowing where to put the timer code and how to properly call the timer function for each enemy.

 

Can anyone help me out on the proper way to use a timer to do this?



[TOPIC: post.html]
#2

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 812 posts
  • Corona SDK

As your code develops you may want need to refine this but for now I would use something like - adding it to your enemy spawn loop

local enemyTimer = timer.performWithDelay( 1000, -- timer is called once per second
    function()
        -- insert code for moving enemy here
        enemy:move( )
    end, 0 ) -- 0 tell the timer to keep repeating forever
enemy.timer = enemyTimer -- This will help you keep track of the timer so you can cancel it when you remove the enemy

 



[TOPIC: post.html]
#3

nml2727

[GLOBAL: userInfoPane.html]
nml2727
  • Enthusiast

  • 96 posts
  • Corona SDK

Thank you for the example code snippet. 

 

So is it pretty standard when you are using timers to write the code for the function directly in the "performWithDelay" like you've shown here?  

 

Or does that have something to do with the way I'm spawning the enemies?



[TOPIC: post.html]
#4

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 812 posts
  • Corona SDK

No, it's just personal preference - it's an inline function.  I often write inline functions to make it easier to pass parameters to the methods I call from the inline function.



[TOPIC: post.html]
#5

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 812 posts
  • Corona SDK

I use it kind of like a palette to mix and match other functions as well.  Like calling a function to analyze an object or value before deciding which function to trigger.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: timer, spawning, objects, timer.performwithdelay