Problems with timer.performWithDelay()

Hi everybody,

Im trying to use a timer.performWithDelay() function in my app.

My problem is:

I would like move a pj across the display automactly (with sprites).

First, move to the left during 1000 ms and after move up during 1000.

Im trying with:

onMoveX(old_x,new_x) – move Left

timer.performWithDelay (1000, onMoveY(old_y,new_y) ) – Launch onMoveY after 1000 ms

and dont function

What is the problem? 

Is mandatiry develop a listener function for each move?

Please help!!

Thanks in advance

Regards

If you want to move display objects across the screen, I suggest you use transitions. These can easily achieve things like “move left, then move up”

See http://docs.coronalabs.com/api/library/transition/to.html

Yes, _memo, im using trasition.to, my problems is the duration of the trasition

I mean, when I move 3 positions (for example) from Left to Right takes 300 ms per move, in total 900 ms.

After that I want to move from up to down

pseudo-code

move right, 300 ms

move right, 300 ms

move right, 300 ms

wait until move right finish (900 ms) --> I would like run MoveRight() after 900 ms

move down, 300 ms

? Transition.to lets you perform onComplete, which is more what you need. That way your up/down movement occurs only when your move right is done.

-- Provided you have a function called doSomethingElse() transition.to( myObject, { time=300, -- time, in ms x=50, -- x position to move to delta=true -- this means that x actually will be myObject.x + x onComplete = doSomethingElse, } -- Or you can use a closure, which is a function without a name transition.to( myObject, { time=300, -- time, in ms x=50, -- x position to move to delta=true -- this means that x actually will be myObject.x + x onComplete = function() transition.to( myObject, { time=300, y=50, delta=true } end, }

not intended to detract from your valid solution, and your comment is more than adequate for casual discussion, but i just got done trying to explain this to someone, so perhaps i’m feeling excessively “nitpicky” at the moment (and so apologies in advance):

“anonymous function” = “function without a name”
“closure” = “function PLUS its environment” (whether named or anonymous)

classic (-ish) example of a named closure:

do local \_print=print print=function(msg) \_print("MY print says: ", msg) end end -- redefine print() -- the redefined print() "remembers" the original \_print() in its closure environment: print("Hello World!") -- but \_print was local, so not available here: \_print("This will generate an error because \_print does not exist in this scope")

fwiw

If you want to move display objects across the screen, I suggest you use transitions. These can easily achieve things like “move left, then move up”

See http://docs.coronalabs.com/api/library/transition/to.html

Yes, _memo, im using trasition.to, my problems is the duration of the trasition

I mean, when I move 3 positions (for example) from Left to Right takes 300 ms per move, in total 900 ms.

After that I want to move from up to down

pseudo-code

move right, 300 ms

move right, 300 ms

move right, 300 ms

wait until move right finish (900 ms) --> I would like run MoveRight() after 900 ms

move down, 300 ms

? Transition.to lets you perform onComplete, which is more what you need. That way your up/down movement occurs only when your move right is done.

-- Provided you have a function called doSomethingElse() transition.to( myObject, { time=300, -- time, in ms x=50, -- x position to move to delta=true -- this means that x actually will be myObject.x + x onComplete = doSomethingElse, } -- Or you can use a closure, which is a function without a name transition.to( myObject, { time=300, -- time, in ms x=50, -- x position to move to delta=true -- this means that x actually will be myObject.x + x onComplete = function() transition.to( myObject, { time=300, y=50, delta=true } end, }

not intended to detract from your valid solution, and your comment is more than adequate for casual discussion, but i just got done trying to explain this to someone, so perhaps i’m feeling excessively “nitpicky” at the moment (and so apologies in advance):

“anonymous function” = “function without a name”
“closure” = “function PLUS its environment” (whether named or anonymous)

classic (-ish) example of a named closure:

do local \_print=print print=function(msg) \_print("MY print says: ", msg) end end -- redefine print() -- the redefined print() "remembers" the original \_print() in its closure environment: print("Hello World!") -- but \_print was local, so not available here: \_print("This will generate an error because \_print does not exist in this scope")

fwiw