Game Development Reference
In-Depth Information
Checking for matches
How do we determine if two pieces were matched by a player? The way it is done
in this implementation is quite straightforward. Each piece already knows which
piece is supposed to be on top of it, on the bottom, and on its sides. When a piece
is moved, we simply check if any of these pieces are close enough within tolerance.
Note that if the dragged piece belongs to a group, then we need to go through every
piece in the group to check whether, for each piece, it got close enough to its correct
neighboring piece.
The check for a match gets triggered when the player moves a piece, or more
specifically, when the player lets go of the piece (mouse up event) at the end
of dragging it.
As noted earlier, when two pieces are matched, the groups the pieces belong to are
merged. It could happen that more than two groups are merged at the same time.
Let's take a look at the mouse up event handler on the PieceSprite class:
private function mouseUpHandler(event:MouseEvent):void {
var sprite:Sprite = Sprite(event.target);
sprite.removeEventListener(MouseEvent.MOUSE_MOVE,
mouseMoveHandler);
sprite.stopDrag();
var finalGroup:Group = null;
var mergeGroup:Group = m_group;
// We merge as many groups there
// are to be merged.
// The final aggregated group is
// pointed to by finalGroup
while (mergeGroup != null ) {
// returns non-null group
// if there was a merge
mergeGroup = mergeGroup.checkMatch();
if ( mergeGroup != null ) {
// save a reference to
// the aggregated group
finalGroup = mergeGroup;
}
}
// Piece match effect
if ( finalGroup != null ) {
_showEffect(finalGroup);
}
}
 
Search WWH ::




Custom Search