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?