Having trouble with the timing and activation of a transition.to() animation

In this game the player builds an expression that generates a pre-defined target number. The user does this by selecting the appropriate tiles to create the expression. After every selection the game will check if the expression equals the target number, award points as necessary and the reset the tiles so the player can go again.

Mechanically they game works but I am having a problem animating the tiles back to their original positions. The problem is that for some reason the last 1 or 2 tiles are not reset to their original positions when resetTiles() is called. The weird thing is that if I call resetTiles() manually it works fine. Please see a video of the problem here:

http://www.youtube.com/watch?v=tJu8w8jO9RY&feature=youtu.be

In this case I need to make the number four so I tap on “2”, “+”, “2” at which point the function checkIfScore() calls resetTiles(). This should make all the tiles return back to their position but in fact you can see that the last selected tile (a “2”) remains at the top of the screen until I manually call resetTiles() again by shaking the device.

Why does resetTiles() work when I call it by shaking the device but not when called within checkIfScore()?

-- This is touch handler function local function handleTileEvent( event ) local phase = event.phase local tile = event.target if not tile.selected then if phase == "began" then tile.imgOver.isVisible = true tile.img.alpha = 0.01 elseif phase == "ended" then --EXPRESSION.text = EXPRESSION.text..target.img.label EXPRESSION.text = EXPRESSION.text..tile.label EXPRESSION.x = EXPRESSION.x tile.selected = true settings.currentScoreToAward = settings.currentScoreToAward + tile.bonusAmount animateTileToExpression(tile) end end end -- This function takes a tile in the grid and animates it to the top of the screen to show that its has been selected local function animateTileToExpression(target) local p = target.pos local t = tilesDG.tiles[tostring(p)] if gd.lastX == nil then gd.lastX = t.scaledWidth end -- Store tiles orginal position so we can animate it back later t.previous = { ["x"] = t.x, ["y"] = t.y } local function checkIfScore () local function f() local total = evalExpression(EXPRESSION.text) if total == tonumber(TARGET.text) then SCORENUM.text = tonumber(SCORENUM.text) + settings.currentScoreToAward SCORENUM.x = SCORENUM.x scoreFx(settings.currentScoreToAward, 120, 220) generateNewTarget() EXPRESSION.text = "" resetTiles() end end timer.performWithDelay(500, f) end transition.to(t, { time = settings.animDuration, x = gd.lastX, y = gd.lastY, transition = easing.outElastic, onComplete = checkIfScore() }) table.insert(expr, t) gd.lastX = gd.lastX + t.scaledWidth end -- This function resets all the tiles to their original positions local function resetTiles () -- Reset starting co-ords for expression gd.lastX = gd.startingX gd.lastY = gd.startingY settings.currentScoreToAward = 0 -- Reset tiles states and move back into position for i = 1, #expr do print(i) local tile = expr[i] tile.selected = false tile.img.alpha = 1.0 tile.imgOver.isVisible = false tile.x = tile.previous.x tile.y = tile.previous.y --[[transition.to(tile, { x = tile.previous.x, y = tile.previous.y, time = 200, transition = easing.outElastic })]]-- end end