Game Development Reference
In-Depth Information
13. The handling of user input is performed in the OnKey() method. When the user
presses the mouse button or taps on the screen, this method is called with the
KeyState argument equal to true. On the mouse release or at the end of the
tap, the OnKey() method is called with KeyState set to false. The mx and my
parameters contain the 2D coordinates of the touch. Once the touch is active, we
store the indices of the tile and the initial offset of the touch point respective to the
upper-left corner of the tile:
void Puzzle::OnKey( float mx, float my, bool KeyState )
{
int i = (int)floor( mx * FColumns );
int j = (int)floor( my * FRows );
int MouseI = ( i >= 0 && i < FColumns ) ? i : -1;
int MouseJ = ( j >= 0 && j < FRows ) ? j : -1;
FMovingImage = KeyState;
if ( FMovingImage )
{
FClickedI = MouseI;
FClickedJ = MouseJ;
if ( FClickedI >= 0
&& FClickedJ >= 0
&& FClickedI < FColumns
&& FClickedJ < FRows )
{
FOfsX = ( ( float )FClickedI / FColumns - mx );
FOfsY = ( ( float )FClickedJ / FRows - my );
}
else
{
FClickedI = FClickedJ = -1;
}
}
else
14. When the touch ends, we check the validity of the new tile position and exchange the
selected tile with the tile in the new position:
{
bool NewPosition = ( MouseI != FClickedI ||
MouseJ != FClickedJ );
bool ValidPosition1 = ( FClickedI >= 0 && FClickedJ >=
0 && FClickedI < FColumns && FClickedJ < FRows );
bool ValidPosition2 = ( MouseI >= 0 && MouseJ >= 0 &&
MouseI < FColumns && MouseJ < FRows );
 
Search WWH ::




Custom Search