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})
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. [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!!!
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]