How do you code a repeated action?

I have a transition.to command that I want to repeat ad nauseum at a rate that can be tweaked and/or randomized. I’m still a newbie, however, so I don’t know how to code repeated actions. It’s just an object crossing the screen from left to right:
scooter1.x = -100scooter1.y = math.random(1,1024)scooter1:play('Scooter 1 moving right')transition.to(scooter1, {time = 1500, delay = 500, x = scooter1.x + 968})[/code]Thank you,Steven [import]uid: 79394 topic_id: 13929 reply_id: 313929[/import]

use

timer.performWithDelay(milliseconds here, your function here, number of times to repeat(0 for infinite)) [import]uid: 16142 topic_id: 13929 reply_id: 51196[/import]

Thanks, I tried changing my code to:

scooter1.x = -100scooter1.y = math.random(1,1024)scooter1:play('Scooter 1 moving right')timer.performWithDelay(1000,transition.to(scooter1, {time = 1500, delay = 500, x = scooter1.x + 968}) , 0)[/code]but no effect. I know I've got the wrong idea here, I feel like a total newbie now.. [import]uid: 79394 topic_id: 13929 reply_id: 51199[/import]

Right you need to add your code into a function.

  
function scooterFunction()  
  
 scooter1.x = -100  
 scooter1.y = math.random(1,1024)  
 scooter1:play('Scooter 1 moving right')  
 transition.to(scooter1, {time = 1500, delay = 500, x = scooter1.x + 968})  
  
end  
  

Then you can use what dark consoles said:

timer.performWithDelay(milliseconds here, scooterFunction, number of times to repeat)  

To tweak the milliseconds that this function is called you can use math.random.

So first setup a new variable. We’ll call it randTime.

 local randTime 

You then want to assign the max and minimum wait time between the function being called. lets say 500 and 10000 milliseconds.

local randTime = math.random( 500, 10000 )

Your finished code will then look like this:

  
local randTime = math.random( 500, 10000 )  
  
function scooterFunction()  
  
 scooter1.x = -100  
 scooter1.y = math.random(1,1024)  
 scooter1:play('Scooter 1 moving right')  
 transition.to(scooter1, {time = 1500, delay = 500, x = scooter1.x + 968})  
  
end  
  
timer.performWithDelay(randTime, scooterFunction, 0)  
  

Any questions feel free to ask.
[import]uid: 68741 topic_id: 13929 reply_id: 51202[/import]

@notts_forest_

Thank you for responding, at the stage I’m at, I really appreciate the walkthrough approach to your answer, something tells me you’ve got teaching exp:)

I’m getting some odd results off your code sugg. The scooter enters from screen left, pops down to a different y coordinate, and then does this several more times before exiting screen right, slowing down with each pop. The timer seems to be causing the scooterFunction to fire at the random intervals intended, but rather than starting a new scooter crossing the screen with every firing (the effect I want), it’s applying itself to the existing scooter that’s already making it’s way across.

I’m trying to make sense of this, but my head’s starting to hurt, any ideas?

Thank you,
Steven [import]uid: 79394 topic_id: 13929 reply_id: 51208[/import]

did you create the scooter inside the function ?

for your requirement you should do something like this
[lua]local randTime = math.random( 500, 10000 )

function scooterFunction()
–scooter should be declare local inside the function to create
–separate instance on each function call
local scooter = display.newImage(“scooter.png”)
scooter.x = -100
scooter.y = math.random(1,1024)
transition.to(scooter, {time = 1500, delay = 500, x = scooter.x + 968})
end

timer.performWithDelay(randTime, scooterFunction, 0)[/lua] [import]uid: 71210 topic_id: 13929 reply_id: 51230[/import]

Hit the nail on the head, it’s working as intended now! So unless the function calls for a new image to be spawned everytime it fires, it will just apply the function repeatedly to the one image spawned outside of the function, I get it! Thanks yet again Renjith for clearing that up, and thanks darkconsoles and notts_forest_ for your help as well:) [import]uid: 79394 topic_id: 13929 reply_id: 51294[/import]