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 );
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 );
[import]uid: 33154 topic_id: 15113 reply_id: 315113[/import]