Are there problems using timer.performWithDelay inside a for statement?

Hi, I’m a beginning programmer and I feel like I’m not understanding something about the for statement or the use of the timer.performWithDelay. If I use the code below as it is, the final iteration (in this example the 5th) iteration of the for statement always bombs. I remove the closures for the objectmove function and the timer and all 5 iterations appear and function as expected. When introducing the timer and the closures however all but the final iteration behave as expected. The square in the final iteration never appears. I have some more elaborate code where I was able to confirm that this iteration is in fact triggering but never becomes visible with the timer implemented to stagger them. I’ve pounded my head against the wall for a month and can find nothing wrong with my code which may be causing this. Is this a limitation of the for statement or timer library? Also I am still currently using the trial version so my only other thought is that this is perhaps a deliberate limitation of the trial version. If it is the case that my code is good and that this is a deliberate limitation due to it being the trial version it would be great to know so that I can move on and get this closer to where I want it to be before subscribing. Kind of hoping to avoid paying for a subscription if I figure out that I’m just not gonna hack it as a coder… Badum dum… lol Thanks all for any help with this!

[lua]    – Global Variables

    

    halfW = display.viewableContentWidth / 2

    halfH = display.viewableContentHeight / 2

    

    – Group Variables

    

    local group = display.newGroup()

    

    – Insert and position Characters

    for i = 1, 5  do

    

        group:insert (display.newRect( 0, 0, 50, 50 ))

        group[i].xScale = .07

        group[i].yScale = .07

        group[i].isVisible = false

        group[i]:setReferencePoint ( display.BottomCenterReferencePoint )

        group[i]:translate( halfW + math.random( -80, 80 ), halfH + math.random( -250, -250 ) )

        local spawntimer = (i*500) + 500

        

        – Move and Scale Characters/Trigger Animation

        timer.performWithDelay( spawntimer , objectmove )

        

        objectmove = function( event )

            

            transition.to( group[i], { time=15000, x = math.random( 40, 500 ) } )

            

            characterYGo = function( event )

                group[i].isVisible = true

                charYpos = group[i].y

                group[i].y = charYpos * 1.002 + 0.5

                ScalePercent = (group[i].y ) / 8 + 4

                group[i].xScale = 0.015 * ScalePercent

                group[i].yScale = 0.015 * ScalePercent

                end

            

            Runtime:addEventListener( “enterFrame”, characterYGo )

            

            end

        end [/lua]

Focusing ONLY on your problem with the timers try this:

  1. – Global Variables
  2.     
  3.     halfW = display.viewableContentWidth / 2
  4.     halfH = display.viewableContentHeight / 2
  5.     
  6.     – Group Variables
  7.     
  8.     local group = display.newGroup()
  9.     
  10.     – Insert and position Characters
  11.     for i = 1, 5  do
  12.     
  13.         group:insert (display.newRect( 0, 0, 50, 50 ))
  14.         group[i].xScale = .07
  15.         group[i].yScale = .07
  16.         group[i].isVisible = false
  17.         group[i]:setReferencePoint ( display.BottomCenterReferencePoint )
  18.         group[i]:translate( halfW + math.random( -80, 80 ), halfH + math.random( -250, -250 ) )
  19.  
  20.         local spawntimer = (i*500) + 500
  21.         
  22.         local objectmove = function( event )
  23.             
  24.             transition.to( group[i], { time=15000, x = math.random( 40, 500 ) } )
  25.             
  26.             characterYGo = function( event )
  27.                 group[i].isVisible = true
  28.                 charYpos = group[i].y
  29.                 group[i].y = charYpos * 1.002 + 0.5
  30.                 ScalePercent = (group[i].y ) / 8 + 4
  31.                 group[i].xScale = 0.015 * ScalePercent
  32.                 group[i].yScale = 0.015 * ScalePercent
  33.                 end
  34.             
  35.             Runtime:addEventListener( “enterFrame”, characterYGo )
  36.             
  37.             end
  38.  
  39.        – Move and Scale Characters/Trigger Animation
  40.         timer.performWithDelay( spawntimer , objectmove )

See line  22 - Made function local

See  lines 40 - Moved performWithDelay() call after.

Your prior code was written as if you had forward declared objectmove, but you didn’t.  Also, you kept redefining it.

The above changes will end up defining five (5) unique versions of the function objectmove() which may or may not be what you want.  This is not really memory efficient and it takes lots of extra processing time to dynamically create these functions, so if you’re going to have big loops this will bite you.

Thank you so much Roaming Gamer!!! You not only helped me to understand dealing with my immediate problem, your additional details help clarify some other issues I have been having. I know now not only how to fix the problem but exactly what was happening to cause the problem to begin with and that was my bigger picture goal!!!

This has not only worked on dealing with this little bump in the road, but has also gotten me past a major roadblock in my understanding and learning process!!!

Focusing ONLY on your problem with the timers try this:

  1. – Global Variables
  2.     
  3.     halfW = display.viewableContentWidth / 2
  4.     halfH = display.viewableContentHeight / 2
  5.     
  6.     – Group Variables
  7.     
  8.     local group = display.newGroup()
  9.     
  10.     – Insert and position Characters
  11.     for i = 1, 5  do
  12.     
  13.         group:insert (display.newRect( 0, 0, 50, 50 ))
  14.         group[i].xScale = .07
  15.         group[i].yScale = .07
  16.         group[i].isVisible = false
  17.         group[i]:setReferencePoint ( display.BottomCenterReferencePoint )
  18.         group[i]:translate( halfW + math.random( -80, 80 ), halfH + math.random( -250, -250 ) )
  19.  
  20.         local spawntimer = (i*500) + 500
  21.         
  22.         local objectmove = function( event )
  23.             
  24.             transition.to( group[i], { time=15000, x = math.random( 40, 500 ) } )
  25.             
  26.             characterYGo = function( event )
  27.                 group[i].isVisible = true
  28.                 charYpos = group[i].y
  29.                 group[i].y = charYpos * 1.002 + 0.5
  30.                 ScalePercent = (group[i].y ) / 8 + 4
  31.                 group[i].xScale = 0.015 * ScalePercent
  32.                 group[i].yScale = 0.015 * ScalePercent
  33.                 end
  34.             
  35.             Runtime:addEventListener( “enterFrame”, characterYGo )
  36.             
  37.             end
  38.  
  39.        – Move and Scale Characters/Trigger Animation
  40.         timer.performWithDelay( spawntimer , objectmove )

See line  22 - Made function local

See  lines 40 - Moved performWithDelay() call after.

Your prior code was written as if you had forward declared objectmove, but you didn’t.  Also, you kept redefining it.

The above changes will end up defining five (5) unique versions of the function objectmove() which may or may not be what you want.  This is not really memory efficient and it takes lots of extra processing time to dynamically create these functions, so if you’re going to have big loops this will bite you.

Thank you so much Roaming Gamer!!! You not only helped me to understand dealing with my immediate problem, your additional details help clarify some other issues I have been having. I know now not only how to fix the problem but exactly what was happening to cause the problem to begin with and that was my bigger picture goal!!!

This has not only worked on dealing with this little bump in the road, but has also gotten me past a major roadblock in my understanding and learning process!!!