【问题标题】:function with transition.to() does not work带有 transition.to() 的函数不起作用
【发布时间】:2013-12-22 17:39:44
【问题描述】:
local rect = display.newRect(100, 100, 100, 100)

local moving, moving2
function moving()
    transition.to(rect, {time=500, x=300, y=100, onComplete=moving2})
end

function moving2()
    transition.to(rect, {time=500, x=100, y=300, onComplete=moving})
end

大家好。我是 Lua 的新手,所以我想知道为什么我的矩形没有在我的屏幕上使用这个功能移动?当我在下面仅使用此代码时,它会移动但最终会停止。我希望它反复从一侧移动到另一侧:

local rect = display.newRect(100, 100, 100, 100)
transition.to(rect, {time=500, x=300, y=100, onComplete=moving2})

【问题讨论】:

  • 您是否尝试调用其中一个函数?
  • 这完全取决于你在什么环境中工作——显示、newRect、过渡等不是语言的标准部分。

标签: function lua coronasdk


【解决方案1】:

您需要调用其中一个函数。简单地说:

moving()

作为最后一行。

【讨论】:

    【解决方案2】:

    就像他们说的,它不动是因为你不打电话 moving() 或者 moving2() 在你的代码中。

    您知道,您不必在 onComplete 参数中使用两个不同的函数来做这些复杂的事情。通过更改对象的缓动函数并将iterations 参数设置为-1 以实现无限循环,您可以通过一次转换对对象产生相同的效果。

    以下是可用缓动函数的列表:http://docs.coronalabs.com/api/library/easing/index.html,正如您所见,easing.continuousLoop 函数可以满足您的需求。

    你可以试试这样的:

    local rect = display.newRect(100, 300, 100, 100)
    transition.to(rect, {
        time = 500, 
        x = 300,
        y = 100,
        iterations = -1,
        transition = easing.continuousLoop,
    })
    

    【讨论】:

      【解决方案3】:

      放在最后 移动() 它工作正常。已经测试过了。或者你可以从 移动2()

      【讨论】:

        【解决方案4】:

        谢谢大家,把moving ()放在最后效果很好

        我已经用easing.continuousLoop 快速尝试了最后一个代码,但它并没有完全按照我的意愿工作,但我稍后会深入检查它,因为它可能会有所帮助

        谢谢大家

        【讨论】:

          【解决方案5】:

          哦,伙计,使用本地对象并从两个转换中调用它有时会导致严重错误,使用您现有的代码将“rect”传递给每个转换,这样它就不会被“仍在过渡。”

          local rect = display.newRect(100, 100, 100, 100)
          
          local moving, moving2
          function moving(inObj)
              if (inObj == nil) then inObj = rect end
              transition.to(inObj, {time=500, x=300, y=100, onComplete = function(iObj)
                   moving2(iObj)
              end})
          end
          
          function moving2(inObj)
              transition.to(inObj, {time=500, x=100, y=300, onComplete, onComplete = function(iObj)
                   moving(iObj)
              end})
          end
          

          如果你只是想偷懒:)

          local rect = display.newRect(100, 100, 100, 100)
          local movingIndex = 0
          local moveData = {
          {time = 500, x = 300, y = 100},
          {time = 500, x = 300, y = 100}
          }
          
          function MoveObject(inObj)
              movingIndex = movingIndex + 1
              if (movingIndex > #moveData) then movingIndex = 1 end
          
              transition.to(inObj, {tag = "movingObjects", time=moveData[movingIndex].time, x=moveData[movingIndex].x, y=moveData[movingIndex].y, onComplete = function(iObj)
                   MoveObject(iObj)
              end})
          end
          
          MoveObject(rect)
          

          这样你就只有一个函数,并且可以将动画点添加到 moveData 表:)

          通过使用 tag = "movingObjects" 标记过渡,我们可以通过一个调用来暂停和恢复任何正在运行的过渡,例如 transition.pause("movingObjects") 或取消等。当您想要暂停和/或移动到时很有用无需等待转换结束或将其包装在 pcall() 中即可看到另一个

          只是思考的食物:)

          btw:: 我没有测试上面的代码,我只是在这个编辑器中编写的,所以可能有 1 到 2 件事需要调整。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多