Lag in game - Accordion Effect - Would using TRANSLATE be less CPU intensive than moving objects by stating the coordinates?

In my game I am moving my ground objects from the right of the screen, to the left of the screen. These ground objects, when placed side by side, take up the horizontal space on the very bottom (baseline) of the screen, creating the “ground”.

Ground - Made up of: 20 sprites, each 64 x 64

These sprites move in unison, from right to left, using this common code.

local tDelta = event.time - tPrevious tPrevious = event.time local xOffset = ( 0.2 \* tDelta ) for i=1, #spriteArray, 1 do spriteArray[i].x = spriteArray[i].x - xOffset end

They move fine. If a sprite moves past the left side of the screen, in this case - 50x, that sprite is then told to move to the far right side of the screen (if sprite.x < -50 then move sprite to far right of screen). Each sprite does this infinite times, making up the ground. 

Problem - sometimes, if stuttering/lag occurs, you see them stack up a little bit on the left side of the screen, and then, leaving a gap, but then corrects itself after the lag stops. This happens on the simulator and on all devices. I am trying to figure out why this happens, but I understand that no matter what I do there will always be unpredictable lag (brief stutter/hiccup/lag from processor trying to compute?), and so I feel there is nothing I can do about it. 

I must find a way around this.

Questions:

  1. Would TRANSLATE be less cpu intensive than moving the sprites like I am doing now?

  2. Is there a better way to move individual sprites, knowing that there will always be a stutter(lag)?. 

  3. Would grouping the objects make it less cpu intensive? (make all 20 sprites part of one group, move group instead of individual sprites). - I don’t know why this would though, just a shot in dark.

I feel this happens on most games I play. Take Flappy Birds, using top of the line phones/tablets, I see brief stuttering/lag with the pipes. As the pipes are moving, you will notice that sometimes they stutter, jumping a minute amount from right to left, or not jumping, but pausing for a tenth of a second. On Physics based games, runners/flyers, I see this all of the time. It seems people are just used to it? I don’t see many complaints about it in the game ratings/comments section. 

This is one of the reasons coding games can be so time consuming. Even if you have perfect code, you still have to deal with making it work on all computers/devices. After all of your code is complete, game ready to launch, all it takes is one little stutter/lag due to processor to make the game flawed.

I think you have a problem with your code there. A CPU lag due to something going on would not cause your sprites to stack up as they would not be moved left while the CPU is busy so you would just see a freeze and then everything would continue as normal without any stacking or gaps. You could get this if you don’t move the sprites to the right in the same frame you move them left. For example if you check for x < -50 first and only after move the sprites left then this would happen. So I suggest you check for x < -50 after you have moved them left according to the time passed. This way no matter how bad the lag is sprites will be in correct position even though the game freezes for a bit.

No harm in putting them in a group and seeing what happens.

I assume your ground is animated and that’s why you can’t just have a 1280 x 64 image?

Figured it out. It definitely was my code. Lag will still exist, but in this case it was my code. It was way more complicated than it needed to be. I do this to myself though cause I try to make everything completely dynamic in my code. Although the answers did not solve my riddle, it made me look at my code in a different way; which, most of the time is all we coders need - that second/third pair of eyes. Thanks gentlemen of Corona

I think you have a problem with your code there. A CPU lag due to something going on would not cause your sprites to stack up as they would not be moved left while the CPU is busy so you would just see a freeze and then everything would continue as normal without any stacking or gaps. You could get this if you don’t move the sprites to the right in the same frame you move them left. For example if you check for x < -50 first and only after move the sprites left then this would happen. So I suggest you check for x < -50 after you have moved them left according to the time passed. This way no matter how bad the lag is sprites will be in correct position even though the game freezes for a bit.

No harm in putting them in a group and seeing what happens.

I assume your ground is animated and that’s why you can’t just have a 1280 x 64 image?

Figured it out. It definitely was my code. Lag will still exist, but in this case it was my code. It was way more complicated than it needed to be. I do this to myself though cause I try to make everything completely dynamic in my code. Although the answers did not solve my riddle, it made me look at my code in a different way; which, most of the time is all we coders need - that second/third pair of eyes. Thanks gentlemen of Corona