[Resolved]transition.to ( ) and external modules

So this is my square.lua

[lua]module(…,package.seeall)

local square = {}
local square_mt = {__index = square}

function square.new()
local newSquare

newSquare = display.newImage(“square.png”)
newSquare:setReferencePoint(display.CenterReferencePoint)
newSquare.x = 10
newSquare.y = 20

return setmetatable(newSquare, square_mt)
end

function square:animate()
transition.to(square, { time=7000, y = 50, onComplete = animate})
end

return square[/lua]

and this is my main.lua

[lua]local square = require(“square”)

local square1 = square.new()
square1:animate()[/lua]

Shouldn’t this work? What am I missing? [import]uid: 106739 topic_id: 18458 reply_id: 318458[/import]

Try modifying your square.lua to add the animate function directly to the new square object when it is created instead of modifying it’s metatable.

module(...,package.seeall)  
  
local square = {}  
local square\_mt = {\_\_index = square}  
  
function square.new()  
 local newSquare  
  
 newSquare = display.newImage("square.png")  
 newSquare:setReferencePoint(display.CenterReferencePoint)  
 newSquare.x = 10  
 newSquare.y = 20  
  
 function newSquare:animate()  
 print("test")  
 transition.to(newSquare, { time=7000, y = 50, onComplete = newSquare.animate})  
 end  
  
 return newSquare  
end  
  
return square  

Nathan [import]uid: 100558 topic_id: 18458 reply_id: 70866[/import]

Hey Nathan, cheers for the reply.

I tried to put some parameters to the function, but it doesn’t like it.

square.lua:
[lua]function square.new(params)
local newSquare, x, y

newSquare = display.newImage(“car.png”)
newSquare:setReferencePoint(display.CenterReferencePoint)
newSquare.x = params.x
newSquare.y = params.y

function newSquare:animate(params)
local x, y
transition.to(newSquare, {time = 7000, x = params.x, y = screenBottom + 30, onComplete = newSquare.animate})
end

return newSquare
end

return square[/lua]

main.lua:

[lua]local square1 = square.new(screenRight/2, screenTop+20)
square1:animate(screenRight, screenTop+20[/lua]

Surely this should be working?

Also, I would like to reset the square after each transition to go back to where it was spawned initially, in main.lua. Do you know how I could do that?

Thanks for your effort and patience :slight_smile: [import]uid: 106739 topic_id: 18458 reply_id: 71008[/import]

It looks like the issue is in how you’re passing the parameters in your main.lua. There are a large number of ways you could handle this but here’s one of the shorter ones to type:

main.lua:

local square1 = square.new( {x = screenRight/2, y = screenTop+20} )  
square1:animate( {x = screenRight, y = screenTop+20} )  

I’ll leave the puzzle of resetting the position mostly up to you, but I will say that you might want to consider using a different onComplete function.
Nathan [import]uid: 100558 topic_id: 18458 reply_id: 71051[/import]

Uh shouldn’t you be animating the actual display object, not the table returned by the new function?

function square:animate() transition.to(newSquare, { time=7000, y = 50, onComplete = animate}) end [import]uid: 19626 topic_id: 18458 reply_id: 71075[/import]

Robmiracle, the “table” returned by the new function -is- the display object. The module square is simply a helper that is used to create a specialize display object. You could modify the module to a much greater extent to get the same results but that comes down to personal preference. [import]uid: 100558 topic_id: 18458 reply_id: 71096[/import]

Thank you Nathan, solved it with a few flags and a new function to reset the position. There must be a more direct way than 2 functions with some flags, but this works for now, so I’m happy :slight_smile: [import]uid: 106739 topic_id: 18458 reply_id: 71542[/import]