Fishies Sample App

Hello, can someone explain in layman terms the following code from the sample App fishies (http://developer.anscamobile.com/sample-code/fishies)?

-- add fish to animation group so that it will bounce  
bounceAnimation[#bounceAnimation + 1] = group  

How are the fish being added to the group? Through a table? What does the # mean in front of #bounceAnimation? [import]uid: 14218 topic_id: 7900 reply_id: 307900[/import]

means length. and since “arrays” in lua are 1-indexed, not 0-indexed, then the initial length is zero and #table+1 will be the first item, the first time around. then the length is 1 so #table+1 will be the 2nd item ( [table[2] )

group is the display group containing the 2 fish versions
[lua]local group = display.newGroup()

group:insert( fishOriginal, true )

group:insert( fishDifferent, true )
…[/lua]

[import]uid: 6645 topic_id: 7900 reply_id: 28083[/import]

So, I guess my next question is how are the fish objects being passed into the for statement for animation?

On line 152-153 of the fish example, http://developer.anscamobile.com/sample-code/fishies, the sample app has the following code:

for i,v in ipairs( self ) do  
local object = v   
-- the display object to animate, e.g. the fish group  

How are the fish objects being passed to the for statement? [import]uid: 14218 topic_id: 7900 reply_id: 28146[/import]

ok…

[lua]function bounceAnimation:enterFrame( event )[/lua]
is a table function. [lua]bounceAnimation[/lua] is the table. [lua]self[/lua] refers to bounceAnimation, because it is the table that the function is being called on. “enterFrame” is a built-in event. this function definition therefore makes the bounceAnimation table respond to the enterFrame events

so [lua] for i,v in ipairs( self ) do[/lua] means go through each of the indexed items in the bounceAnimation table (indexed from 1 to #bounceAnimation… 1,2,3 etc like an array)

we know from the previous question that these items in the array contain “group” which is the display group for each fish, containing it’s 2 different images (orange fish and blue fish)

from the [lua]ipairs[/lua] statement, [lua]i[/lua] is the index (1,2,3) and [lua]v[/lua] is the value at [lua]bounceAnimation[i][/lua] which is the fish “group”

and this statement:[lua]Runtime:addEventListener( “enterFrame”, bounceAnimation );[/lua]
means on every enterFrame event (30 or 60 times a second), call the bounceAnimation function. except in our case bounceAnimation isn’t a function, it is a table. therefore it means call the bounceAnimation:enterFrame function. (which you saw we defined above)
[import]uid: 6645 topic_id: 7900 reply_id: 28154[/import]