Does anybody has simple code to move an object back and forth always or up and down.
Thanks [import]uid: 11559 topic_id: 23745 reply_id: 323745[/import]
Hey there, just threw this together for you - it’s for up and down but could be changed to left and right just by changing the Y to X.
[lua]local obj = display.newCircle( 50, 50, 30 )
local trans2
local function trans1 ()
t1 = transition.to(obj, {time=2000, y=430, onComplete=trans2})
end
trans2 = function ()
t2 = transition.to(obj, {time=2000, y=50, onComplete=trans1})
end
trans1()[/lua]
Peach
[import]uid: 52491 topic_id: 23745 reply_id: 95552[/import]
I would just change the above code so it becomes easier to cancel the transition. Since when you call for one of them, the other is finished, I would advise to use the same variable to hold it, like:
local obj = display.newCircle( 50, 50, 30 )
local trans2
local t1
local function trans1 ()
t1 = transition.to(obj, {time=2000, y=430, onComplete=trans2})
end
trans2 = function ()
t1= transition.to(obj, {time=2000, y=50, onComplete=trans1})
end
trans1()
This way when you need to cancel the going back and forth you just need to do:
transition.cancel(t1)
Also note that you can’t have multiple objects being transitioned with this, because you will just be storing 1 single pointer to one of the transitions and you then wouldn’t be able to cancel them all if you wish.
You could go around that using a table to store all ongoing transitions:
local obj = display.newCircle( 50, 50, 30 )
local trans2
local ts = {}
local index = 1
local function trans1 (obj)
ts[obj.tIndex] = transition.to(obj, {time=2000, y=430, onComplete=trans2})
end
trans2 = function (obj)
ts[obj.tIndex] = transition.to(obj, {time=2000, y=50, onComplete=trans1})
end
obj.tIndex = index
trans1(obj)
index = index + 1
So basicaly you keep an index for each obj you set to go back and forth. You store this index on the object itself so it knows the number of its transition. [import]uid: 61899 topic_id: 23745 reply_id: 95559[/import]
Thank you very much all of you! [import]uid: 11559 topic_id: 23745 reply_id: 95589[/import]