Iterating through display group and rotating objects

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:

[lua]for item in newDisplayGroup do
if (item.type == “ball”) then
item.rotation = item.rotation + 5
end
end[/lua]

I keep getting ‘attempt to call a table value’, I’m normally a python guy so not being able to do this confuses me. [import]uid: 88429 topic_id: 17842 reply_id: 317842[/import]

Hey there, here’s an example I quickly wrote up for you - not the neatest but it works;

[lua]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[/lua]

Obviously I’m using a PNG called “rock” - but if you try this in a new project and throw a random image in there it should work well.

Peach :slight_smile: [import]uid: 52491 topic_id: 17842 reply_id: 68095[/import]

PS - I didn’t use an if statement in the example but you easily could, just to be clear :slight_smile: [import]uid: 52491 topic_id: 17842 reply_id: 68097[/import]

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. [import]uid: 88429 topic_id: 17842 reply_id: 68106[/import]

Not a problem, I love questions where I can provide some simple code to help explain the solution :smiley:

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 :wink:

Peach :slight_smile: [import]uid: 52491 topic_id: 17842 reply_id: 68118[/import]