local widget = require(“widget”)
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
_W = display.contentWidth
_H = display.contentHeight
puzzlePiecesCompleted = 0
totalPuzzlePieces = 9
puzzlePieceWidth = 120
puzzlePieceHeight = 120
puzzlePieceStartingY = {
80,220,360,500,640,780,920,1060,1200 }
puzzlePieceSlideUp = 140
puzzleWidth, puzzleHeight = 320, 320
puzzlePieces = {}
puzzlePieceCheckpoint = {
{x=-243,y=76},
{x=-160, y=76},
{x=-76, y=74},
{x=-243,y=177},
{x=-143, y=157},
{x=-57, y=147},
{x=-261,y=258},
{x=-176,y=250},
{x=-74,y=248}
}
puzzlePieceFinalPosition = {
{x=77,y=75},
{x=160, y=75},
{x=244, y=75},
{x=77,y=175},
{x=179, y=158},
{x=265, y=144},
{x=58,y=258},
{x=145,y=251},
{x=248,y=247}
}
playGameGroup = display.newGroup()
finishGameGroup = display.newGroup()
function shuffle(t)
local n = #t
while n > 2 do
local k = math.random(n)
t[n] = t[k]
t[k] = t[n]
n = n - 1
end
return t
end
local function onDrag(event)
local t = event.target
if event.phase == “began” then
t.xScale, t.yScale = 1.3, 1.3
t:toFront()
display.getCurrentStage():setFocus( t )
t.isFocus = true
xOrigin,yOrigin = t.x,t.y
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == event.phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == event.phase
or “cancelled” == event.phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
if t.x <= puzzlePieceCheckpoint[t.id].x+30
and t.x >= puzzlePieceCheckpoint[t.id].x-30
and t.y <= puzzlePieceCheckpoint[t.id].y+30
and t.y >= puzzlePieceCheckpoint[t.id].y-30 then
puzzleGroup:insert(t)
t:toBack()
t.place = “moved”
t.x, t.y = puzzlePieceFinalPosition[t.id].x,
puzzlePieceFinalPosition[t.id].y
t:removeEventListener(“touch”,onDrag)
for i = 1, totalPuzzlePieces do
if puzzlePieces[i].place == “start”
and puzzlePieces[i].y > yOrigin then
puzzlePieces[i].y = puzzlePieces[i].y - puzzlePieceSlideUp
end
end
puzzlePiecesCompleted = puzzlePiecesCompleted+1
if puzzlePiecesCompleted == totalPuzzlePieces then
playGameGroup.isVisible = false
finishGameGroup.isVisible = true
end
else
t.xScale,t.yScale = 1,1
t.x, t.y = xOrigin, yOrigin
end
end
end
return true
end
function scene:createScene( event )
local group = self.view
puzzleGroup = display.newGroup()
woodBoard = display.newImageRect(“woodboard.png”, 480, 320)
woodBoard.x = 562
woodBoard.y = _H/2
woodBoard:toBack();
puzzleGroup:insert(woodBoard)
scrollView = widget.newScrollView
{
top=0,
left = _W - 160,
height=_H,
width = 160,
hideBackground = true,
scrollWidth = 50 ,
scrollHeight = 1000
}
local shufflePuzzleY = shuffle(puzzlePieceStartingY)
for i = 1, totalPuzzlePieces do
puzzlePieces[i] = display.newImageRect(i…".png", puzzlePieceWidth, puzzlePieceHeight)
puzzlePieces[i].x = 85
puzzlePieces[i].y = shufflePuzzleY[i]
puzzlePieces[i].id = i
puzzlePieces[i].place = “start”
puzzlePieces[i]:addEventListener(“touch”, onDrag)
scrollView:insert(puzzlePieces[i])
end
puzzleGroup:insert(scrollView)
playGameGroup:insert(puzzleGroup)
function returnToMenu(event)
if(event.phase == “ended”) then
storyboard.gotoScene(“menu”)
end
end
txt_gameComplete = display.newText(“Puzzle Complete”, 0,0,120,100,native.systemFont,22)
txt_gameComplete.x = 400
txt_gameComplete.y = _H/2 - 50
finishGameGroup:insert(txt_gameComplete)
txt_return = display.newText(“Return to Menu”, 0,0,120,100,native.systemFont,22)
txt_return.x, txt_return.y = 400, _H/2 + 50
txt_return:addEventListener(“touch”, returnToMenu)
finishGameGroup:insert(txt_return)
baseBG = display.newImageRect(“puzzle-base.png”, puzzleWidth, puzzleHeight)
baseBG.x, baseBG.y = 160, _H/2
finishGameGroup:insert(baseBG)
group:insert(finishGameGroup)
group:insert(playGameGroup)
finishGameGroup.isVisible = false
playGameGroup.isVisible = true
end
function scene:enterScene( event ) end
function scene:exitScene( event ) end
function scene:destroyScene( event ) end
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )
return scene