moving objects created with a loop

 

Hi, I have created several objects in a loop. I am trying to move the objects but only one object is moving. I know I need to move them through the table i placed them in but iam kind of stuck. I have stripped down the  code would somebody cast a quick eye on it please?

I am missing something simple.

[lua]

local function createFollower(layer)

    local layer = layer or display.currentStage

    for i = 1, 10 do

        local follower = display.newCircle(0, 0, 5)

        follower.x = math.random(0, 320) --display.contentWidth * 0.5

        follower.y = math.random(0, 480) --display.contentHeight * 0.5

        follower.name = “follower”

        physics.addBody(follower, { isSensor = true, radius = 20 })

        follower:addEventListener(“collision”, follower)

       

        followers[#followers + 1] = follower

 function moveFollowers()

        local playerTravel

        local multiplier

        local playerTravel = player.x

        local multiplier = -1

        if player.x <= 450 then scrollSpeedX = 0

        else

            scrollSpeedX = (playerTravel / 600) * multiplier

        end

        if followers[#followers].x

        then

            followers[#followers].x =

            followers[#followers].x + scrollSpeedX

        end

        if player.x <= 50 then playerTravel = 480 - player.x

        multiplier = 1

        scrollSpeedX = (playerTravel / 300) * multiplier

        end

        if followers[#followers].x

        then

            followers[#followers].x =

            followers[#followers].x + scrollSpeedX

        end

    end

    Runtime:addEventListener(“enterFrame”,moveFollowers)

end

[/lua]

In your moveFollowers() function, you are only calling the amount of objects indexed in your  followers table. You need to loop through the entire table inside that function. Something like:

for i=1, #followers do if followers[i].x then

if you want to speed up your code dont use

  1. followers[#followers].x =
  2.             followers[#followers].x + scrollSpeedX

use translate.

and why using runtime to move objects? you can do it without it. transitions or/and timers comes on my mind…

Thanks Alex, makes sense now.

Carlos I often use an enterframe to move stuff rather than a timer or transitions.

I can also flag objects for removal by keeping an enterframe listener to watch them. :slight_smile:

In your moveFollowers() function, you are only calling the amount of objects indexed in your  followers table. You need to loop through the entire table inside that function. Something like:

for i=1, #followers do if followers[i].x then

if you want to speed up your code dont use

  1. followers[#followers].x =
  2.             followers[#followers].x + scrollSpeedX

use translate.

and why using runtime to move objects? you can do it without it. transitions or/and timers comes on my mind…

Thanks Alex, makes sense now.

Carlos I often use an enterframe to move stuff rather than a timer or transitions.

I can also flag objects for removal by keeping an enterframe listener to watch them. :slight_smile: