Bezier curve in Transition Function

I am trying to make a game where it will choose two hats randomly out of 5, then make them curve above and below each other. 

[lua]

hat1 = display.newImage(“KNBR_Images/KNBR_hat.png”)

hat1:setReferencePoint(display.CenterReferencePoint)

hat1.y = 250

hat1.x = 132

gamePage:insert(hat1)

hat2 = display.newImage(“KNBR_Images/KNBR_hat.png”)

hat2:setReferencePoint(display.CenterReferencePoint)

hat2.y = 250

hat2.x = 326

gamePage:insert(hat2)

hat3 = display.newImage(“KNBR_Images/KNBR_hat.png”)

hat3:setReferencePoint(display.CenterReferencePoint)

hat3.y = 250

hat3.x = 510

hat3.name = ‘finalHat’

gamePage:insert(hat3)

hat4 = display.newImage(“KNBR_Images/KNBR_hat.png”)

hat4:setReferencePoint(display.CenterReferencePoint)

hat4.y = 250

hat4.x = 694

gamePage:insert(hat4)

hat5 = display.newImage(“KNBR_Images/KNBR_hat.png”)

hat5:setReferencePoint(display.CenterReferencePoint)

hat5.y = 250

hat5.x = 892

gamePage:insert(hat5)

ball = display.newImage(“KNBR_Images/KNBR_ballSm.png”)

ball.x = hat3.x

ball.y = hat3.y

gamePage:insert(ball)

hats = display.newGroup( hat1, hat2, hat3, hat4, hat5)

gamePage:insert(hats)

playBtn = display.newImage(“KNBR_Images/KNBR_buttonPlay.png”)

playBtn:setReferencePoint(display.CenterReferencePoint)

playBtn.x = display.contentCenterX

playBtn.y = 460

gamePage:insert(playBtn)

–Start Movement---------------------------------------------------------------------------------------

function upHatAnimation()

    transition.to( hat3, { time = 500, y = hat3.y - 150})

    local function downHatAnimation ()

        transition.to( hat3, { time = 500, y = hat3.y + 150, onComplete = randomHatMove})

    end

    timer.performWithDelay( 3000, downHatAnimation, 1)

    playBtn.alpha = 0.5

    playBtn:removeEventListener(“tap”, upHatAnimation)

    textBoxStart.isVisible = false

    textBoxBlank.isVisible = true

    textBoxRight.isVisible = false

    textBoxWrong.isVisible = false

    textBoxPick.isVisible = false

    playBtn:removeEventListener(“tap”, playBtn)

    --backArrow:removeEventListener(“tap”, gotoGame)

end

playBtn:addEventListener(“tap”, upHatAnimation)

–Movement---------------------------------------------------------------------------------------

function randomHatMove()

    local randm1 = math.floor(math.random(1,5))

    local randm2 = math.floor(math.random(1,5))

    

    print("h1 Before change: "…randm1)

    print("h2 Before change: "…randm2)

    if (randm1 == randm2) then

        if (randm1 == 1 ) then

            randm1 = randm1 + 1

            print(“Problem: Add”)

        elseif (randm1 >= 2) then

            randm1 = randm1 - 1

            print(“Problem: Subtract”)

        end

        --print(“Same”)

    end

    

    print("h1 After change: "…randm1)

    print("h2 After change: "…randm2)

    local h1 = hats[randm1]

    local h2 = hats[randm2]

    if ( h1 == h2 ) then

        print(“Problem”)

    end

    totalMoves = totalMoves - 1

    moveNumber = moveNumber + 1

    

    h1.t = 0 – Give this to one of the objects

    transition.to(h1, { time = moveSpeed, rotation = rotation, t = 1, transition = easing.inQuad }) – Interpolate the time

    transition.to(h2, { time = moveSpeed, rotation = rotation_negative, transition = easing.inQuad, onComplete = checkMovesLeft })

    

    – Cache the state as upvalues.

    local x1, y1 = h1.x, h1.y

    local x2, y2 = h2.x, h2.y

    local midx, midy = .5 * (x1 + x2), .5 * (y1 + y2)

    local to_midx, to_midy = midx - x1, midy - y1

    – Perpendiculars: Depends how the points are laid out, assuming leftish to rightish

    local overx, overy = midx - to_midy, midy + to_midx

    local underx, undery = midx + to_midy, midy - to_midx

    – Give this to the one with the “onComplete” logic

    function hatTimer()

      – Coefficients

      local t = h1.t

      local s = 1 - t

      local a = s * s

      local b = 2 * s * t

      local c = t * t

      h1.x, h1.y = x1 * a + overx * b + x2 * c, y1 * a + overy * b + y2 * c

      h2.x, h2.y = x2 * a + underx * b + x1 * c, y2 * a + undery * b + y1 * c

    end

    Runtime:addEventListener(“enterFrame”, hatTimer)

    --timer.performWithDelay(5, hatTimer, 15)

    ball.isVisible = false

    print("Total Moves: "…moveNumber)

    print("--------------")

end

–playBtn:addEventListener(“tap”, randomHatMove)

function checkMovesLeft(h)

    function checkMovesDelay ()

        if(totalMoves > 0) then

            randomHatMove()

            --Runtime:addEventListener(“enterFrame”, hatTimer)             

        else

            hat1:addEventListener(‘tap’, revealBall)

            hat2:addEventListener(‘tap’, revealBall)

            hat3:addEventListener(‘tap’, revealBall)

            hat4:addEventListener(‘tap’, revealBall)

            hat5:addEventListener(‘tap’, revealBall)

            Runtime:removeEventListener(“enterFrame”, hatTimer) 

            --Runtime:removeEventListener(“enterFrame”, randomNumbersPick)    – Kill the timer

            totalMoves = 15

        end

    end

    timer.performWithDelay(100, checkMovesDelay, 1)

end

[/lua]

The problem is randomly it will pick two or more hats to move, thus messing up the checkMoves() counter. Help!