Game Development Reference
In-Depth Information
if (_tileList[newIndex].letter != CrosswordTile.
EMPTY) _tileList[newIndex].dispatchEvent(new
MouseEvent(MouseEvent.CLICK));
break;
case Keyboard.LEFT:
newIndex = Math.max(selectedIndex - 1, (_selected
Tile.tileIndex.y * _puzzleWidth));
if (_tileList[newIndex].letter != CrosswordTile.
EMPTY) _tileList[newIndex].dispatchEvent(new
MouseEvent(MouseEvent.CLICK));
break;
case Keyboard.RIGHT:
newIndex = Math.min(selectedIndex + 1,
((_selectedTile.tileIndex.y+1) * _puzzleWidth)-1);
if (_tileList[newIndex].letter != CrosswordTile.
EMPTY) _tileList[newIndex].dispatchEvent(new
MouseEvent(MouseEvent.CLICK));
break;
case Keyboard.SPACE:
_selectedTile.dispatchEvent(new MouseEvent
(MouseEvent.CLICK));
break;
default:
_selectedTile.setAnswer(e);
break;
}
}
The keyDown method is responsible for handling a few
different types of keyboard inputs. We employ a switch statement
to filter through the possible values for the key that was pressed.
In addition to responding to alphabetic key presses, we want to
give the player the ability to move between different tiles with the
arrow keys, as well as the ability to toggle between the across and
down words of the selected tile. For the arrow key input, if a tile
in the direction the player is attempting to move isn
tblacked
out, we simulate a mouse click by dispatching a new MouseEvent
from the tile. The result is that it
'
s as though the tile next to the
selected tile was clicked with the mouse and selectTile is called to
handle it. By simulating already existing functionality, we lessen
the possibility for bugs, since the logic on selection of words
based on tiles is centralized in one place. The same is true for
the space bar; when it is pressed it is as though the selected tile
was simply clicked again. For all other keys, we send the event
through the setAnswer method of the tile. If you recall, that
method knows how to filter for proper alphabetic inputs, so we
don
'
'
thavetoworryaboutthathere.
Search Nedrilad ::




Custom Search