I have a display group that is filled with images, I need to be able to rotate each one of these objects inside a special function which will be part of the game loop so that the image is rotating.
I have not been able to find a way to iterate through all items in the display group like so:
1 2 3 4 5 | for item in newDisplayGroup do if (item.type == "ball") then item.rotation = item.rotation + 5 end end |
Hey there, here's an example I quickly wrote up for you - not the neatest but it works;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | local localGroup = display.newGroup() local rock1 = display.newImage("rock.png") rock1.x = 50 rock1.y = 50 localGroup:insert( rock1 ) local rock2 = display.newImage("rock.png") rock2.x = 100 rock2.y = 100 localGroup:insert( rock2 ) local rock3 = display.newImage("rock.png") rock3.x = 150 rock3.y = 150 localGroup:insert( rock3 ) for i = 1, localGroup.numChildren do localGroup[i].rotation = localGroup[i].rotation + 5 end |
PS - I didn't use an if statement in the example but you easily could, just to be clear :)
ahh thank you soo much. It looks like one of the big things I forgot was that Lua starts arrays at 1 and Python starts at 0.
Not a problem, I love questions where I can provide some simple code to help explain the solution :D
The whole 0/1 thing I know confuses a lot of people, I'm lucky because I didn't know any other languages before learning Lua - although if I learn others in the future I'm sure I'll make similar mistakes ;)
Peach :)