Function loop while firing it again

Hey, I’m just practicing my coding skills by making a classic space-asteroid shooting game. (classy)

And the problem I’ve encountered is probably just too complicated for me to understand either Corona engine or the language itself. What I’m trying to do is to loop a function that creates asteroids, that loops a function that moves and rotates them, but every time a new asteroid is created, the loop for moving stops for the previous one. Here’s my code:
[lua]

local function moveAsteroid() – function for coroutine loop

if asteroid then

asteroid.y = asteroid.y+1

asteroid.rotation = asteroid.rotation + 2-- + x

end end

local function func() – looping the moveAsteroid

timer.performWithDelay(1, moveAsteroid, 0)

end

local function doit() – resuming the coroutine

local co = coroutine.create(func) – coroutine for the loop

coroutine.resume(co)

end

function newAsteroid()

asteroid = display.newImageRect(“asteroid.png”, 90, 90)

–physics.addBody(asteroid, {bodyType=“kinematic”, bounce=false})

asteroid.x = math.random(90, (display.contentWidth-90))

asteroid.y= -40

doit()  – must be fired once per asteroid and stay until asteroid == nil

end

timer.performWithDelay(1000, newAsteroid, 0) – looping newAsteroid

[/lua]

what happens in simulator: asteroids appear, while they live, everything is fine, but as soon as the next asteroid appears, the previous one stops, the new one gains double speed for moving and rotation and stops when the next one is created. Loops.

Help?

Hi @qwertyuk01,

Just curious… are you sure you want to use coroutines? That’s a fairly advanced concept and it’s not necessary for most developers. If you understand how to use them, and you’re comfortable with learning the intricacies, then go ahead, but I just want to caution you on that.

Anyway, your current code is spawing more than one asteroid, but each time you spawn one, you’re using the same variable reference (“asteroid”) for the new one. That’s why the old one’s behavior gets messed up.

Solutions are potentially numerous:

  1. You could “attach” a repeating timer to each asteroid and then refer back to that asteroid on each firing of the timer to make it move/rotate, but that potentially gets confusing, and it creates a lot of timers.

  2. You could do a repeating transition on the rotation part, and a single transition on the movement part… that would be clean and easy.

  3. You could insert every asteroid into a table and then loop through the table on a Runtime basis, moving and rotating each one.

  4. Use physics as outlined in the Star Explorer tutorial (which is ironically an Asteroids-like game :))

Hope this helps,

Brent

Hi @qwertyuk01,

Just curious… are you sure you want to use coroutines? That’s a fairly advanced concept and it’s not necessary for most developers. If you understand how to use them, and you’re comfortable with learning the intricacies, then go ahead, but I just want to caution you on that.

Anyway, your current code is spawing more than one asteroid, but each time you spawn one, you’re using the same variable reference (“asteroid”) for the new one. That’s why the old one’s behavior gets messed up.

Solutions are potentially numerous:

  1. You could “attach” a repeating timer to each asteroid and then refer back to that asteroid on each firing of the timer to make it move/rotate, but that potentially gets confusing, and it creates a lot of timers.

  2. You could do a repeating transition on the rotation part, and a single transition on the movement part… that would be clean and easy.

  3. You could insert every asteroid into a table and then loop through the table on a Runtime basis, moving and rotating each one.

  4. Use physics as outlined in the Star Explorer tutorial (which is ironically an Asteroids-like game :))

Hope this helps,

Brent