Game Development Reference
In-Depth Information
These methods are what are used on beginning and completion
of a game. Calling activate will enable each piece and make it click-
able. The deactivate method reverses this action, and the cleanUp
function calls the same method on the source image that we
looked at earlier in the interface.
protected function pieceClicked(e:MouseEvent):void {
var piece:IGamePiece = e.target as IGamePiece;
if (!_selectedPiece) {
_selectedPiece = piece;
} else if (_selectedPiece == piece) {
_selectedPiece.deselect();
_selectedPiece = null;
} else {
var index:int = _selectedPiece.currentIndex;
dispatchEvent(new Event(PIECE_SWAP));
piece.deselect();
_selectedPiece.deselect();
movePiece(_selectedPiece, piece.currentIndex);
checkPiece(_selectedPiece);
movePiece(piece, index);
checkPiece(piece);
_selectedPiece = null;
checkWin();
}
}
protected function checkPiece(piece:IGamePiece):Boolean {
if (piece.currentIndex == piece.index) {
piece.removeEventListener(MouseEvent.CLICK,
pieceClicked);
piece.lock();
dispatchEvent(new Event(PIECE_LOCK));
return true;
}
return false;
}
protected function checkWin():void {
var won:Boolean = true;
for each (var piece:IGamePiece in _pieces) {
if (piece.currentIndex != piece.index) won = false;
}
if (won) {
deactivate();
dispatchEvent(new Event(GAME_OVER));
}
}
Search Nedrilad ::




Custom Search