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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 );

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 );

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 );

Sorry, I haven't been able to work on this for a few days until now. Thank you so much, that solved it!

views:1360 update:2011/9/25 12:40:18
corona forums © 2003-2011