Newbie question

So I’m starting out with this framework and have a question.  If you run my project, I’m calling the ‘dog’ class and want it to continue to animate to a new location every time it’s transition is complete.  What’s happening is that I’m seeing only one animate.  I’m confused as to why ‘mySprite’ doesn’t seem to be its own object and why all 10 aren’t moving.  

I think I’m just missing a concept.  Thanks.

EDIT: I just loaded the code into corona and realized what you are trying to do…I’m working on the problem :slight_smile:

Thanks man.

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>

Ha!  I see.  Interesting.  I’m coming from Actionscript 3 so it’s taking some time to get used to how things are scoped in Lua.

Thank you for your help.

Haha glad I could help!

EDIT: I just loaded the code into corona and realized what you are trying to do…I’m working on the problem :slight_smile:

Thanks man.

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>

Ha!  I see.  Interesting.  I’m coming from Actionscript 3 so it’s taking some time to get used to how things are scoped in Lua.

Thank you for your help.

Haha glad I could help!