The hats now move properly, but the ball pops up under the wrong hat! Im about to go crazy on this one.
[lua]
–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---------------------------------------------------------------------------------------
local currentHat1, currentHat2
function randomHatMove()
local randm1 = math.floor(math.random(1,5))
local randm2 = math.floor(math.random(1,5))
if (randm1 == randm2) then
if (randm1 == 1 ) then
randm1 = randm1 + 1
elseif (randm1 > 1) then
randm1 = randm1 - 1
end
end
local h1 = hats[randm1]
local h2 = hats[randm2]
totalMoves = totalMoves - 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 some position state (since the positions themselves change as the curve updates).
x1, y1 = h1.x, h1.y
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
overx, overy = midx - to_midy, midy + to_midx
underx, undery = midx + to_midy, midy - to_midx
– Tell the enterFrame listener which hats we’re watching.
currentHat1, currentHat2 = h1, h2
ball.isVisible = false
end
–playBtn:addEventListener(“tap”, randomHatMove)
function checkMovesLeft(h)
function checkHatMovement ()
if totalMoves > 0 then
– Coefficients
local t = currentHat1.t
local s = 1 - t
local a = s * s
local b = 2 * s * t
local c = t * t
currentHat1.x, currentHat1.y = x1 * a + overx * b + x2 * c, y1 * a + overy * b + y2 * c
currentHat2.x, currentHat2.y = x2 * a + underx * b + x1 * c, y2 * a + undery * b + y1 * c
end
end
Runtime:addEventListener(“enterFrame”, checkHatMovement)
if(totalMoves > 0) then
timer.performWithDelay(100, randomHatMove, 1)
else
hat1:addEventListener(‘tap’, revealBall)
hat2:addEventListener(‘tap’, revealBall)
hat3:addEventListener(‘tap’, revealBall)
hat4:addEventListener(‘tap’, revealBall)
hat5:addEventListener(‘tap’, revealBall)
Runtime:removeEventListener(“enterFrame”, checkHatMovement)
totalMoves = 15
end
end
–Reveal Ball---------------------------------------------------------------------------------------
function revealBall(e)
textBoxBlank.isVisible = false
textBoxPick.isVisible = true
hat1:removeEventListener(‘tap’, revealBall)
hat2:removeEventListener(‘tap’, revealBall)
hat3:removeEventListener(‘tap’, revealBall)
hat4:removeEventListener(‘tap’, revealBall)
hat5:removeEventListener(‘tap’, revealBall)
ball.x = hat3.x + 130
ball.y = hat3.y + 250
ball.isVisible = true
if ( e.target.name == ‘finalHat’ ) then
textBoxPick.isVisible = false
textBoxRight.isVisible = true
transition.to( hat3, { time = 500, y = hat3.y - 150})
local function downHatAnimation_end ()
transition.to( hat3, { time = 500, y = hat3.y + 150})
playBtn:addEventListener(“tap”, upHatAnimation)
playBtn.alpha = 1
end
timer.performWithDelay( 1500, downHatAnimation_end, 1)
else
textBoxPick.isVisible = false
textBoxWrong.isVisible = true
transition.to( hat3, { time = 500, y = hat3.y - 150})
local function downHatAnimation_end ()
transition.to( hat3, { time = 500, y = hat3.y + 150})
playBtn.alpha = 1
playBtn:addEventListener(“tap”, upHatAnimation)
--backArrow:addEventListener(“tap”, gotoGame)
end
timer.performWithDelay( 1500, downHatAnimation_end, 1)
end
end
[/lua]
Any idea whatsoever?