Problem with translating images in a table

I have a table of about 30 small images and I am trying to have them all randomly move around the screen but only the first image is moving around, the rest show up on the screen but don’t move anywhere. My code is below. The print statement on line 3 shows me that it is correctly cycling through all the for loops so that makes me think that I’m not doing something correctly with the translate function. Any help would be greatly appreciated.

local tPrevious = system.getTimer()  
local numImages = 30  
local image = {}  
local xdirection = {}  
local ydirection  = {}  
local xpos = {}  
local ypos = {}  
local xrand = {}  
local yrand = {}  
   
for i=1,numImages do  
        image[i] = display.newImage("image.png")  
        xdirection[i] = 1  
        ydirection[i] = 1  
        xpos[i] = math.random(0,display.contentWidth)  
        ypos[i] = math.random(0,display.contentHeight)  
        xrand[i] = 0  
        yrand[i] = 0  
end  
   
local function animate(event)  
        for i=1,numImages do  
        print(i)  
                xrand[i] = math.random(-100,20)  
                yrand[i] = math.random(-100,20)  
   
                local tDelta = event.time - tPrevious  
                tPrevious = event.time  
          
                xpos[i] = xpos[i] + ( 0.124\*xdirection[i]\*tDelta );  
                ypos[i] = ypos[i] + ( 0.086\*ydirection[i]\*tDelta );  
   
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (xpos[i] \> display.contentWidth - 5 or xpos[i] \< 5 or xrand[i] \> 0) then  
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xdirection[i] = xdirection[i] \* -1;  
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end  
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (ypos[i] \> display.contentHeight - 5 or ypos[i] \< 5 or yrand[i] \> 0) then  
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ydirection[i] = ydirection[i] \* -1;  
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end  
&nbsp;  
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;image[i]:translate( xpos[i] - image[i].x, ypos[i] - image[i].y)  
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;end  
end  
&nbsp;  
Runtime:addEventListener( "enterFrame", animate );  
  

[import]uid: 33154 topic_id: 15113 reply_id: 315113[/import]

I think here enterFrame triggers faster than the time it takes to complete the code inside animate function. So the translation actually happens only to the last item added. I just tried slowing down the things a bit to see the behavior.
try the code below
[lua]local tPrevious = system.getTimer()
local numImages = 3
local image = {}
local xdirection = {}
local ydirection = {}
local xpos = {}
local ypos = {}
local xrand = {}
local yrand = {}

for i=1,numImages do
image[i] = display.newImage(“image.png”)
xdirection[i] = 1
ydirection[i] = 1
xpos[i] = math.random(0,display.contentWidth)
ypos[i] = math.random(0,display.contentHeight)
xrand[i] = 0
yrand[i] = 0
end

local function animate(event)
for i=1,numImages,1 do
print(i)
xrand[i] = math.random(-100,20)
yrand[i] = math.random(-100,20)

local tDelta = event.time - tPrevious
tPrevious = event.time

xpos[i] = xpos[i] + ( 0.124*xdirection[i] * tDelta );
ypos[i] = ypos[i] + ( 0.086*ydirection[i] * tDelta );

if (xpos[i] > display.contentWidth - 5 or xpos[i] < 5 or xrand[i] > 0) then
xdirection[i] = xdirection[i] * -1;
end
if (ypos[i] > display.contentHeight - 5 or ypos[i] < 5 or yrand[i] > 0) then
ydirection[i] = ydirection[i] * -1;
end
image[i].x = xpos[i] - image[i].x
image[i].y = ypos[i] - image[i].y
–image[i]:translate( xpos[i] - image[i].x, ypos[i] - image[i].y)
end
end

timer.performWithDelay(200, animate ,0)
–Runtime:addEventListener( “enterFrame”, animate );[/lua] [import]uid: 71210 topic_id: 15113 reply_id: 55963[/import]

I found the problem… the change in x and y value is too small to notice any change in position.
try using the below code
[lua]local tPrevious = system.getTimer()
local numImages = 30
local image = {}
local xdirection = {}
local ydirection = {}
local xpos = {}
local ypos = {}
local xrand = {}
local yrand = {}

for i=1,numImages do
image[i] = display.newImage(“image.png”)
xdirection[i] = 1
ydirection[i] = 1
xpos[i] = math.random(0,display.contentWidth)
ypos[i] = math.random(0,display.contentHeight)
xrand[i] = 0
yrand[i] = 0
end

local function animate(event)
for i=1,numImages,1 do
print(i)
xrand[i] = math.random(-100,20)
yrand[i] = math.random(-100,20)

local tDelta = 15
–event.time - tPrevious
–tPrevious = event.time

xpos[i] = xpos[i] + ( 0.124*xdirection[i] * tDelta );
ypos[i] = ypos[i] + ( 0.286*ydirection[i] * tDelta );

if (xpos[i] > display.contentWidth - 5 or xpos[i] < 5 or xrand[i] > 0) then
xdirection[i] = xdirection[i] * -1;
end
if (ypos[i] > display.contentHeight - 5 or ypos[i] < 5 or yrand[i] > 0) then
ydirection[i] = ydirection[i] * -1;
end
image[i]:translate( xpos[i] - image[i].x, ypos[i] - image[i].y)
end
end

Runtime:addEventListener( “enterFrame”, animate );[/lua] [import]uid: 71210 topic_id: 15113 reply_id: 55981[/import]

Sorry, I haven’t been able to work on this for a few days until now. Thank you so much, that solved it! [import]uid: 33154 topic_id: 15113 reply_id: 56179[/import]