Search code examples
luacoronasdk

How to use the onComplete in lua, corona SDK


I'm having trouble figuring out what th mistake, that I'm making is. I would be very glad if you could help me. I want one transition.to() happen after the other.

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()

Solution

  • What is your problem?

    Try (tested)

    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()
    

    Before you use function name you must declare variable with the same name or put function with body part above place where you first time use it.

    If you want call move function again after transition.to in move2 completed just use code below

    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()
    

    Notice that you get infinity transitions. Read more about scopes for functions on Corona blog.