2013-12-22 36 views
0
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的新手,所以我想知道为什么我的矩形没有在这个函数的屏幕上移动?当我仅使用下面的代码时,它会移动,但在最后停止。我希望它从一侧重复移动到另一侧:功能与transition.to()不起作用

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

您是否尝试调用其中一个函数? –

+0

这完全取决于你在什么环境中工作---显示,newRect,转换等不是语言的标准部分。 –

回答

2

您需要调用其中一个功能。只要放:

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, 
}) 
0

你可以尝试这样的事情吧。已经测试过它。或者您可以通过 moving2()

0

感谢大家,它的工作原理以及把moving()

我赶紧试了最后一个代码与easing.continuousLoop开始,但它不完全工作,我想,但我要深刻后来检查它,因为它可能会有所帮助

反正感谢所有

0

哦,使用本地对象,从两个转换调用它是在某些时候会造成一个严重的错误,使用现有的代码通对每个转换的“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 

OR

如果你只是想偷懒:)

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 =“moveObjects”标记转换,我们可以暂停并恢复任何正在运行的转换,只需转换一次调用即可。暂停(“移动物体”)或取消等。有用的,当你想暂停和/或迁移到另一个见,而不必等待过渡结束或通过在PCALL()

就食品包装它的思想:)

BTW ::我没有测试上面的代码,我只是在这个编辑器中编写代码,所以可能有1或2个事情需要修改。