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 | 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 ); |
just a quick question, what are
xpos, ypos, xdirection, ydirection, image, xrand, yrand, are they *all* arrays?
cheers,
?:)
Yes, they are all arrays.
Here is the whole 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 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 ); |
views:1465 update:2011/10/4 8:06:35