如何在lua和corona SDK中使用onComplete

我在解决问题时遇到了困难。我很高兴您能帮助我。我想让一个transition.to()在另一个之后发生。

local ball = display.newCircle(160,0,30)

local function move()
ball.x = display.contentWidth/2
ball.y = display.contentWidth-display.contentWidth-ball.contentWidth*2
transition.to(ball, {x=display.contentWidth/2, y=display.contentHeight*1.3, time=5000, onComplete=move2})
end

local function move2()
ball.x = display.contentWidth+ball.contentWidth/2
ball.y = 0-ball.contentWidth/2
transition.to(ball, {x=0-ball.contentWidth/2, y=display.contentHeight+ball.contentWidth/2, time = 5000})
--transition.to(ball,{x=160,y=240})
end

move()
点赞
用户7026995
用户7026995

你的问题是什么?

尝试(已测试)

local ball = display.newCircle(160,0,30)

local function move2()
    ball.x = display.contentWidth + ball.width * 0.5
    ball.y = -ball.width * 0.5
    transition.to(ball, {x=-ball.width * 0.5, y=display.contentHeight + ball.width * 0.5, time = 5000})
end

local function move()
    ball.x = display.contentWidth * 0.5
    ball.y = -ball.width * 2
    transition.to(ball, {y=display.contentHeight * 1.3, time=5000, onComplete=move2})
end

move()

在使用函数名称之前,您必须声明具有相同名称的变量或将函数与体部分放置在您第一次使用它的位置之上。

如果您想在“move2”中的“transition.to”完成后再次调用“move”函数,请使用以下代码

local ball = display.newCircle(160,0,30)

local move

local function move2()
    ball.x = display.contentWidth + ball.width * 0.5
    ball.y = -ball.width * 0.5
    transition.to(ball, {x=-ball.width * 0.5, y=display.contentHeight + ball.width * 0.5, time = 5000, onComplete=move})
end

 function move()
    ball.x = display.contentWidth * 0.5
    ball.y = -ball.width * 2
    transition.to(ball, {y=display.contentHeight * 1.3, time=5000, onComplete=move2})
end

move()

请注意,您将获得无限转换。阅读有关Corona [博客](https://coronalabs.com/blog/2011/09/21/tutorial-scopes-for-functions/)中函数范围的更多信息。

2016-11-27 17:02:44