DHTML Jigsaw Puzzle: IE4; The Controls | WebReference

DHTML Jigsaw Puzzle: IE4; The Controls

Logo

The DHTML Lab Jigsaw Puzzle, Part II: IE4 cont'd
the controls


Of all the controls, we have discussed the Solve button, the Grid button and the Break button in detail. In this page, we will look at the other buttons and pull-down SELECT menus.

The Hint Button

The Hint button solves one piece automatically. The piece selected is the next-in-line to be solved. Clicking the Hint button calls the giveHint() function:

    function giveHint() { if (isBroken) { solve(false) }; }

If the puzzle is not broken, giveHint() does nothing. If it is, solve() is called with an argument of false. This will make solve()'s local isFullSolve variable false, as well, causing only one piece to be repositioned. See the discussion on solve() for more details.

The Drag ON/OFF Button

As a bonus feature, we've included draggability for the complete puzzle object, elPuzzle. However, a user might want to turn this feature off. If you recall, we created the isPuzzDraggable variable to track puzzle draggability. The Drag ON/OFF button calls the dragToggle() function:

    function dragToggle(){ isPuzzDraggable = !isPuzzDraggable; if (!isBroken) { elPuzzle.draggable = !elPuzzle.draggable }; if (isPuzzDraggable) { elPuzzle.style.cursor = "move"; dragBut.value = "Drag ON"; } else { elPuzzle.style.cursor = "default"; dragBut.value = "Drag OFF"; } }

When a puzzle is broken, it is not draggable whether we allow draggability or not. Therefore, dragToggle() first reverses the value of isPuzzDraggable. If the puzzle is not broken, it reverses the value of the puzzle's draggable property as well. Whenever the draggable property is set by other functions, they first check for a true value of isPuzzDraggable.

Finally, the cursor shapes and the display of the button are modified to reflect the change in draggability.

The SELECT Menus

At any time during puzzle play, the piece count can be changed. If a change is made in the Across: or Down: menus, their onChange event handler sets the value of three variables:

    <SELECT ID="selAcross" onChange="puzzAcross = options[selectedIndex].value; isNewPuzz = true; isCreated = false;"> <OPTION VALUE=2>2 <OPTION VALUE=3>3 ... <OPTION VALUE=9>9 </SELECT> <SELECT ID="selDown" onChange="puzzDown = options[selectedIndex].value; isNewPuzz = true; isCreated = false;"> <OPTION VALUE=2>2 <OPTION VALUE=3>3 ... <OPTION VALUE=9>9 </SELECT>

The three variables are set but will only make a difference if the puzzle is broken. The menu options may be changed over and over. It is only when the Break button is clicked that the variables affect the puzzle. The breakUp() function uses them to determine whether the piece elements need to be created or resized. This procedure is discussed in great detail in our previous column.

Conversely, the image drop-down menu causes an immediate update when its value is changed:

    <SELECT ID="selPic" onChange="document.images['imOrig'].src = options[selectedIndex].value; initPuzz();"> <OPTION VALUE="hands.jpg">Hands <OPTION VALUE="reptiles.jpg">Reptiles ... <OPTION VALUE="treereflec.jpg">Tree Reflection </SELECT>

The puzzle image src is changed and a new image begins loading. The initPuzz() function is called, which as we know cleans up any unsolved pieces via the allDone() function, and then initializes the puzzle from scratch.

That about wraps up our discussion of the puzzle code. The next page has yet another instance of a working puzzle, and a short summary.


Produced by Peter Belesis and

All Rights Reserved. Legal Notices.
Created: Nov. 13, 1997
Revised: Jan. 18, 1998

URL: http://www.webreference.com/dhtml/column9/puzzCtrl.html