有没有更好的方法来链接 Corona Lua 中的转换?

发布于 2024-12-06 08:28:46 字数 406 浏览 1 评论 0原文

我目前正在使用以下代码在有东西被拖放到垃圾桶上时对垃圾桶进行动画处理:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

送你一个梦 2024-12-13 08:28:46

好吧,您可以通过设置带有延迟的第二个转换来在单个函数中完成此操作;请参阅此代码示例: 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).

倾城°AllureLove 2024-12-13 08:28:46

您可以通过编写与第一个转换调用内联的 onComplete 函数来完成此操作:

animateTrash = function()
    transition.to(
        trash,
        { time=100, xScale=1.2, yScale=1.2, onComplete=
            function()
                transition.to(
                    trash,
                    {time=100, xScale=1, yScale=1})
            end
        })
end

这不会变得更加高效,但它确实保持了与垃圾桶动画相关的一切都集中在一处。不过,我认为这种方法可能很快就会失控,特别是如果您在动画垃圾桶时执行除转换之外的任何操作,例如,更新程序的状态,或创建/销毁其他对象等。

呼应 jhocking 的答案,这种方法也不支持取消。

You can do it by coding the onComplete function inline with the first transition call:

animateTrash = function()
    transition.to(
        trash,
        { time=100, xScale=1.2, yScale=1.2, onComplete=
            function()
                transition.to(
                    trash,
                    {time=100, xScale=1, yScale=1})
            end
        })
end

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.

零度° 2024-12-13 08:28:46

这个问题很老了,但由于我试图做同样的事情,我想我应该分享我的想法。这会顺序执行作为变量 args 传入的转换。如果提供了 onComplete,则在转换完成时调用它。

local function transitionSequence(target, step, ...)
  local remaining_steps = {...}
  if #remaining_steps > 0 then
    local originalOnComplete = step.onComplete
    step.onComplete = function(target)
      if originalOnComplete then
        originalOnComplete(target)
      end
      transitionSequence(target, unpack(remaining_steps))
    end
    transition.to(target, step)
  else
    transition.to(target, step)
  end
end

例子:

transitionSequence(myImage,
  {xScale=0.5, onComplete=function(t) print("squeeze") end},
  {xScale=1, onComplete=function(t) print("relax") end},
  {yScale=2, onComplete=function(t) print("stretch") end},
  {yScale=1, onComplete=function(t) print("relax again") end})

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.

local function transitionSequence(target, step, ...)
  local remaining_steps = {...}
  if #remaining_steps > 0 then
    local originalOnComplete = step.onComplete
    step.onComplete = function(target)
      if originalOnComplete then
        originalOnComplete(target)
      end
      transitionSequence(target, unpack(remaining_steps))
    end
    transition.to(target, step)
  else
    transition.to(target, step)
  end
end

Example:

transitionSequence(myImage,
  {xScale=0.5, onComplete=function(t) print("squeeze") end},
  {xScale=1, onComplete=function(t) print("relax") end},
  {yScale=2, onComplete=function(t) print("stretch") end},
  {yScale=1, onComplete=function(t) print("relax again") end})
∞觅青森が 2024-12-13 08:28:46
transition.to( trash, {time=t, delta=true, xScale=1.5, transition=easing.continousLoop} )

对于这样的目的也非常有用:

easing.sin = function( f, a )
  return function(t, tmax, start, d)
    return start + delta + a*math.sin( (t/tmax) *f * math.pi*2 )
  end
end

easing.sinDampened = function( f, a, damp )
  return function(t, tmax, start, d)
    return start + delta + a*math.sin( damp^(t/tmax) *f * math.pi*2 )
  end
end

...等

transition.to( trash, {time=t, delta=true, xScale=1.5, transition=easing.continousLoop} )

Also very useful for purposes like that:

easing.sin = function( f, a )
  return function(t, tmax, start, d)
    return start + delta + a*math.sin( (t/tmax) *f * math.pi*2 )
  end
end

easing.sinDampened = function( f, a, damp )
  return function(t, tmax, start, d)
    return start + delta + a*math.sin( damp^(t/tmax) *f * math.pi*2 )
  end
end

...etc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文