使用transition.to()函数不起作用。

本地矩形 = display.newRect(100, 100, 100, 100)

本地移动, 本地移动2 function 本地移动() transition.to(本地矩形, {time=500, x=300, y=100, onComplete=本地移动2}) end

function 本地移动2() transition.to(本地矩形, {time=500, x=100, y=300, onComplete=本地移动}) end

嗨,大家好。我是 Lua 的菜鸟,想知道为什么我的矩形在这个函数中没有动?当我仅使用下面的代码时,它会动但在结束时停止。我希望它可以不断地从一侧移动到另一侧:

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

你需要调用其中一个函数。只需将以下内容放在最后一行:

moving()
2013-12-23 01:44:59
用户1436844
用户1436844

就像他们说的那样,它没有移动是因为你没有在你的代码中调用 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,
})
2013-12-23 10:12:33
用户1925928
用户1925928

使用 moving() 函数放在最后后,它可以很好地运行。已经进行过测试。或者您可以使用 moving2() 开始。

2013-12-24 09:19:48
用户3127418
用户3127418

感谢所有人,将 moving() 放在最后很好用。

我已经快速尝试了最后一个代码,使用 easing.continuousLoop,但它的效果不完全符合我的需求。不过我稍后还会深入检查它,因为它可能会有用。

无论如何,还是要感谢大家。

2013-12-24 17:03:22
用户3131159
用户3131159

哦,使用本地对象并从两个转换调用它可能会导致严重错误,使用您现有的代码将“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()中时非常有用。

仅供思考 :)

顺便说一句:我没有测试上面的代码,我只是在这个编辑器中编写它,可能需要微调1或2个东西。

2013-12-26 16:11:10