The handlers so far have been in the card script, the plan being to have different cards with different types of puzzle. The interactivity handlers can be placed in the stack script available for all the cards.
global snapdistance on preopenstack if the platform contains "iphone" then iphoneUseDeviceResolution true end preopenstack on openstack set the compositorType of this stack to "Static OpenGL" end openstack
preopenstack
handler checks whether the app is on iPhone and requests that the device's native resolution be used. This will make sure that Retina displays show the best quality. The compositorType
being set to "Static OpenGL"
will help performance.on touchStart touchid put the short name of the target into n if word 1 of n is "p" then set the dropShadow of image n to the dropShadow of button "fromlibrary" set the relayerGroupedControls to true set the layer of the target to the number of images in group "pieces" end if end touchstart
relayerGroupedControls
and layer functions to make that piece appear above the other pieces.dropShadow
to the puzzle piece, we create the illusion that the piece is floating above the screen.touchMove
event:on touchMove touchid,touchx,touchy put the short name of the target into n if word 1 of n is "p" then set the loc of the target to touchx,touchy end if end touchMove
on touchEnd touchid put the short name of the target into n if word 1 of n is "p" then checkdistance the short name of the target set the dropShadow of the target to empty checkfinished end if end touchEnd
checkdistance
handler and a distance
function that it calls:on checkdistance dt if snapdistance is empty then put 100 into snapdistance put distance(item 1 of the loc of image dt - item 1 of the myloc of image dt,item 2 of the loc of image dt - item 2 of the myloc of image dt) into d if d<snapdistance then put max(.1,min(.2,d/200)) into t move image dt to the myloc of image dt in t seconds set the relayerGroupedControls to true set the layer of image dt to 2 end if end checkdistance function distance dx,dy return sqrt(dx*dx+dy*dy) end distance
distance
function is based on the Pythagorean theorem, returning the number of pixels between the puzzle piece and its original myloc
value. snapdistance
is the global variable that is used to determine whether the piece is close enough to its starting place to be considered on target.move
line uses LiveCode's move function, which animates the piece into its place.on checkfinished repeat with a = 1 to the number of images in group "pieces" if the myloc of image a of group "pieces" <> the loc of image a of group "pieces" then exit checkfinished end repeat answer "You've done it!" end checkfinished
The jigsaw puzzle should fully work now. Something that you can't easily guess from the touch functions we added is the fact that it works with multitouch. You can drag on up to 10 pieces at once (or whatever the multitouch limit is for your device). Here, each piece will show a drop shadow and all the pieces will animate into their place when you let go of them.
Functions that relate to the puzzle itself are in the card script. Try making a new card that has bigger puzzle pieces and a higher value for snapdistance
(the higher the value, the easier it is to get a piece into place). You could make an opening card for the stack that has a set of difficulty level buttons, one of which would jump to the easier puzzle. This would be ideal for younger players.
Adding some guide graphics will help players know where the edges of the finished puzzle are, and for simpler difficulty levels, you can even include outlines of the individual puzzle pieces.