有没有更好的方法来链接 Corona Lua 中的转换?
我目前正在使用以下代码在有东西被拖放到垃圾桶上时对垃圾桶进行动画处理:
local trashUp
local trashDown
trashUp = function()
transition.to(
trash, {time=100, xScale=1.2, yScale=1.2, onComplete=trashDown })
end
trashDown = function()
transition.to(
trash, {time=100, xScale=1, yScale=1})
end
然后当我想启动动画时调用trashUp()。
该代码工作正常,但我不禁觉得它可以编码得更好。两个函数可以使物体动起来!
有什么办法可以更有效地做到这一点吗?
I'm currently animating a trash can when something gets dragged and dropped onto it with this code:
local trashUp
local trashDown
trashUp = function()
transition.to(
trash, {time=100, xScale=1.2, yScale=1.2, onComplete=trashDown })
end
trashDown = function()
transition.to(
trash, {time=100, xScale=1, yScale=1})
end
and then calling trashUp() when I want to start the animation.
The code works fine, but I can't help feel it could be coded better. Two functions to animate an object!
Is there any way I can do this more efficiently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,您可以通过设置带有延迟的第二个转换来在单个函数中完成此操作;请参阅此代码示例: http://developer.anscamobile.com/reference/index/transitionto
然而,根据您的情况,这不一定不那么复杂,因为现在您必须同时跟踪两个转换,而不是一次只跟踪一个转换。在您发布的代码中,您没有跟踪过渡,但您可能应该这样做,以防在过渡完成之前需要取消它们(例如,播放器在过渡中间切换场景)。
Well you could do it in a single function by setting the second transition with delay; refer to this code example: http://developer.anscamobile.com/reference/index/transitionto
Depending on your situation however that's not necessarily less complicated, because now you have to keep track of two transitions simultaneously instead of just one transition at a time. In the code you posted you aren't keeping track of the transitions, but you probably should be in case you need to cancel them before the transition is complete (eg. the player switches scenes in the middle of the transition).
您可以通过编写与第一个转换调用内联的
onComplete
函数来完成此操作:这不会变得更加高效,但它确实保持了与垃圾桶动画相关的一切都集中在一处。不过,我认为这种方法可能很快就会失控,特别是如果您在动画垃圾桶时执行除转换之外的任何操作,例如,更新程序的状态,或创建/销毁其他对象等。
呼应 jhocking 的答案,这种方法也不支持取消。
You can do it by coding the
onComplete
function inline with the first transition call:This won't be any more efficient, but it does keep everything to do with animating the trashcan in one place. I think this approach could get quickly out of hand, though, especially if you were doing anything other than the transitions when animating the trashcan, e.g., updating your program's state, or creating/destroying other objects, etc.
Echoing jhocking's answer, this approach doesn't support canceling either.
这个问题很老了,但由于我试图做同样的事情,我想我应该分享我的想法。这会顺序执行作为变量 args 传入的转换。如果提供了 onComplete,则在转换完成时调用它。
例子:
This question is pretty old, but since I was trying to do the same thing, I figured I should share what I came up with. This sequentially executes transitions that are passed in as variable args. If onComplete is supplied, it is called as its transition completes.
Example:
对于这样的目的也非常有用:
...等
Also very useful for purposes like that:
...etc