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:

1
2
3
4
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})

use

timer.performWithDelay(milliseconds here, your function here, number of times to repeat(0 for infinite))

Thanks, I tried changing my code to:

1
2
3
4
scooter1.x = -100
scooter1.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)

Right you need to add your code into a function.

1
2
3
4
5
6
7
8
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

@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

did you create the scooter inside the function ?

for your requirement you should do something like this

1
2
3
4
5
6
7
8
9
10
11
12
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)

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:)

views:1455 update:2011/9/27 8:54:05
corona forums © 2003-2011