Rotate loop only works for the last object created

function addStar(\_x,\_y) local star = display.newImage("star2.png") star.x = \_x; star.y = \_y; --star.x = 325; star.y = 200; function starDanceB() transition.to( star, { time=1000, rotation=-20, onComplete=starDanceA()} ) end function starDanceA() transition.to( star, { time=1000, rotation=20, onComplete=starDanceB()} ) end ...

I create three stars, and only the third star maintains the “rotating loop” throughout the game. The first two stars iterate through the loop once and then stop (i.e., rotate -20 degrees and then 20 degrees and stop).

Is there a way to make it so that a rotation loop can be added to any item created on the go with a function and not just the most recent object?

What do startDanceA and startDanceB look like?  

Hi @glchriste,

You may want to simplify this process to use one onComplete handling function. For example:

[lua]

local function handleStar( star )

   if ( star.direction == “counterClockwise” ) then

      transition.to( star, { time=star.transTime*2, rotation=star.rotDeg, onComplete=handleStar } )

      star.direction = “clockwise”

   else

      transition.to( star, { time=star.transTime*2, rotation=-star.rotDeg, onComplete=handleStar } )

      star.direction = “counterClockwise”

   end

end

local function addStar( _x,_y )

   local star = display.newImage( “star.png” )

   star.x = _x; star.y = _y;

   --star.x = 325; star.y = 300;

   local time = 1000

   local rot = 20

   transition.to( star, { time=time, rotation=-rot, onComplete=handleStar } )

   star.direction = “counterClockwise”

   star.transTime = time

   star.rotDeg = rot

end

addStar()

[/lua]

Hope this helps,

Brent

That fixed it! Thank you very much, Brent.

What do startDanceA and startDanceB look like?  

Hi @glchriste,

You may want to simplify this process to use one onComplete handling function. For example:

[lua]

local function handleStar( star )

   if ( star.direction == “counterClockwise” ) then

      transition.to( star, { time=star.transTime*2, rotation=star.rotDeg, onComplete=handleStar } )

      star.direction = “clockwise”

   else

      transition.to( star, { time=star.transTime*2, rotation=-star.rotDeg, onComplete=handleStar } )

      star.direction = “counterClockwise”

   end

end

local function addStar( _x,_y )

   local star = display.newImage( “star.png” )

   star.x = _x; star.y = _y;

   --star.x = 325; star.y = 300;

   local time = 1000

   local rot = 20

   transition.to( star, { time=time, rotation=-rot, onComplete=handleStar } )

   star.direction = “counterClockwise”

   star.transTime = time

   star.rotDeg = rot

end

addStar()

[/lua]

Hope this helps,

Brent

That fixed it! Thank you very much, Brent.