[SOLVED]onComplete not triggering at the right time

Hey all!

My code has my ship rotating for a total of 3 seconds (3000 in the below function)

  
function movementEnded(passedShip)  
 passedShip.stillMoving = 0   
 print("We got to movement ended!")  
end  
  
function turnRight(passedShip)  
 local degreeAmount = 30  
 local targetRotation = passedShip.rotation + degreeAmount  
 passedShip.stillMoving = 1  
 transition.to(passedShip, {time=3000, rotation=targetRotation, onComplete=movementEnded(passedShip)})  
end  
  

But the output from my “movementEnded” function prints IMMEDIATELY instead of after the designated 3 seconds that it’s supposed to take from my “turnRight” function. What am I doing wrong?

Any help is appreciated!

Thanks!
Mario [import]uid: 11636 topic_id: 4774 reply_id: 304774[/import]

Any time you write () the function is called immediately. So don’t write out () in the onComplete parameter.

That however means you can’t send a parameter to the function. The way to handle this is by taking an object-oriented approach, where the onComplete parameter is set to the object and not a function, and that object has an onComplete method, like so:

function passedShip:onComplete()  
self.stillMoving = 0  
print("We got to movement ended!")  
end  
transition.to(passedShip, {time=3000, rotation=targetRotation, onComplete=passedShip})  

In the documentation note that it says onComplete can be a function or table listener; I’m talking about setting up a table listener:
http://developer.anscamobile.com/content/animation#transition.to_target_params_ [import]uid: 12108 topic_id: 4774 reply_id: 15247[/import]

Dude I guess I better give that article you recently posted a good going over!! Thanks for the tip, I found half the problem on this thread:

http://developer.anscamobile.com/forum/2010/10/29/transition-oncomplete-seems-fire-too-soon

but the whole assign it to the object passed thing is still a bit over my head! [import]uid: 11636 topic_id: 4774 reply_id: 15248[/import]

There’s probably a way to get the specific ship without using OOP but I don’t know what that way is, and you should be doing OOP anyway because that’s a cleaner way of programming. [import]uid: 12108 topic_id: 4774 reply_id: 15250[/import]

You’re preaching to the choir! I love OOP! But LUA is one strange animal as far as “faking” OOP goes! Square peg, sorta round hole as it were. :slight_smile: [import]uid: 11636 topic_id: 4774 reply_id: 15252[/import]

Actually, I find doing OOP in Lua much more natural and seamless than other languages, but that may be because I’ve used enough different languages to like the flexibility. Most people would find Lua’s flexibility sloppy and prefer the more prescribed approach of a language like Java. For example, the fact that objects in Lua are really tables is quite a head trip, since it’s not like you can assign functions to array elements in most languages.

ADDITION: oh wait are you the same guy as the “finding a parent” thread? I only realized how natural and seamless OOP is in Lua after that thread. I should start paying attention to who’s who. [import]uid: 12108 topic_id: 4774 reply_id: 15253[/import]

Hahaha! Yeah it’s me! The “Finding an object’s parent” guy! TBH I STILL have to sit down and read that new thread you posted, but it’s just so damned FUN coding away!!!

:slight_smile: [import]uid: 11636 topic_id: 4774 reply_id: 15262[/import]

passedShip will actually be the argument to your transition complete function

[lua]function movementEnded(obj)
– note obj *is* passedShip
– ie the object you called the transition on

obj.stillMoving = 0
print(“We got to movement ended!”)
end

function turnRight(passedShip)
local degreeAmount = 30
local targetRotation = passedShip.rotation + degreeAmount
passedShip.stillMoving = 1
transition.to(passedShip, {time=3000, rotation=targetRotation, onComplete=movementEnded})
end[/lua]

obviously you could change [lua]obj[/lua] to [lua]passedShip[/lua] if you really want

note also if you need to do similar with a timer, you can pass parameters in the timer object to the completion function:

http://developer.anscamobile.com/forum/2010/08/19/it-possible-pass-parameters-event-completion-routine#comment-11546

[import]uid: 6645 topic_id: 4774 reply_id: 15383[/import]

huh, that’s good to know, I missed that in the documentation. Doesn’t matter for me with the way I code everything as methods, but that’s a nifty trick to know about. [import]uid: 12108 topic_id: 4774 reply_id: 15386[/import]

it’s not in the docs. i’ve just added a comment though
http://developer.anscamobile.com/reference/index/transitionto#comment-15513 [import]uid: 6645 topic_id: 4774 reply_id: 15514[/import]