Alright, finally found the problem. It was a scope issue where the animateSprite function was being called once on each dog, but then only looped on the last dog that was created. What I did to solve it was add the animate function to the dog module and then made it so that it animates any sprite that is passed through it (in dog.lua):
[lua]
dog.animateSprite = function(sprite)
transition.to( sprite, { time=math.random( 500, 5000 ), x=math.random( 0, 320 ), y=math.random( 0, 480 ), transition=easing.outQuad, onComplete=dog.animateSprite } )
end
[/lua]
Then, in the dog.new function, I connected mySprite to the newDog table so that it can be called in main.lua. I’ll just show the whole dog.new function to give context (still dog.lua):
[lua]
function dog.new(name) – constructor
local newDog = {
name = name or “Unnamed”
}
mySprite = display.newImage( “characters/dog.png” )
mySprite.x = math.random( 0, 320 )
mySprite.y = math.random( 0, 480 )
mySprite.width = 50
mySprite.height = 50
newDog.sprite = mySprite
return setmetatable( newDog, dog_mt )
end
[/lua]
And then in main.lua we can now use the animate function with the dog’s sprite passed through it:
[lua]
for i=0, 10, 1 do
local dog1 = dog.new( “Yai-Ya” … i)
dog.animateSprite(dog1.sprite)
end
[/lua]
hope this helps!</p>